vxworks pthread porting code
|
1 /* pthreads implementation, by Craig Vanderborgh, craigv@voxware.com) */
2
3 /*---------------------------------------------------------------------------*
4 * DESCRIPTION
5 * Initialize a condition variable
6 *---------------------------------------------------------------------------*/
7 {
8 if (!cond)
9 return PTHR_FAILURE;
10
11 cond->defunct = TRUE;
12 if ((cond->waitersMsgQ = msgQCreate(PTHR_MAX_WAITERS, sizeof(Int),
13 MSG_Q_FIFO)) == NULL)
14 return PTHR_FAILURE;
15 cond->defunct = FALSE;
16 return PTHR_SUCCESS;
17 }
18
19 Int
20 pthread_cond_destroy(pthread_cond_t * cond)
21 /*---------------------------------------------------------------------------*
22 * DESCRIPTION
23 * Destroy a condition variable
24 *---------------------------------------------------------------------------*/
25 {
26 Int tid;
27
28 if (!cond)
29 return PTHR_FAILURE;
30
31 cond->defunct = TRUE;
32 while (msgQNumMsgs(cond->waitersMsgQ) > 0) {
33 if (msgQReceive(cond->waitersMsgQ, (char *)&tid, sizeof(Int),
34 WAIT_FOREVER) == ERROR) {
35 VBX_print("ERROR: pthread_cond_signal can't read waiter tid\n");
36 return PTHR_FAILURE;
37 }
38 if (taskResume(tid) == ERROR) {
39 VBX_print("ERROR: pthread_cond_signal can't restart task %d\n", tid);
40 return PTHR_FAILURE;
41 }
42 }
43
44 if (msgQDelete(cond->waitersMsgQ) == ERROR) {
45 VBX_print("ERROR: pthread_cond_destroy can't delete msgQ!\n");
46 return PTHR_FAILURE;
47 }
48
49 return PTHR_SUCCESS;
50 }
51
52 Int
53 pthread_cond_wait(pthread_cond_t * cond , pthread_mutex_t * mutex)
54 /*---------------------------------------------------------------------------*
55 * DESCRIPTION
56 * Wait on a condition variable
57 *---------------------------------------------------------------------------*/
58 {
59 Int tid, status;
60
61 if (!cond || !mutex || cond->defunct)
62 return PTHR_FAILURE;
63
64 tid = taskIdSelf();
65
66 if (msgQSend(cond->waitersMsgQ, (char *) &tid, sizeof(Int),
67 WAIT_FOREVER, MSG_PRI_NORMAL) == ERROR) {
68 VBX_print("ERROR: pthread_cond_wait can't place taskId on waiters msgQ\n");
69 return PTHR_FAILURE;
70 }
71
72 if (pthread_mutex_unlock(mutex) != PTHR_SUCCESS) {
73 VBX_print("ERROR: pthread_cond_wait can't unlock mutex %x\n", mutex);
74 return PTHR_FAILURE;
75 }
76
77 if ((status = taskSuspend(tid)) != OK) {
78 VBX_print("ERROR: pthread_cond_wait can't suspend task 5d\n", tid);
79 return PTHR_FAILURE;
80 }
81
82 if (pthread_mutex_lock(mutex) != PTHR_SUCCESS) {
83 VBX_print("ERROR: pthread_cond_wait can't reacquire mutex %x\n", mutex);
84 return PTHR_FAILURE;
85 }
86
87 return PTHR_SUCCESS;
88 }
89
90 Int
91 pthread_cond_timedwait(pthread_cond_t * cond , pthread_mutex_t * mutex,
92 const struct timespec * abstime)
93 /*---------------------------------------------------------------------------*
94 * DESCRIPTION
95 * Cop-out version of a timed condition variable wait
96 *---------------------------------------------------------------------------*/
97 {
98 Int tid, status;
99
100 if (!cond || !mutex || !abstime)
101 return PTHR_FAILURE;
102
103 if (pthread_mutex_unlock(mutex) != PTHR_SUCCESS) {
104 VBX_print("ERROR: pthread_cond_wait can't unlock mutex %x\n", mutex);
105 return PTHR_FAILURE;
106 }
107
108 pthread_delay_np(abstime);
109
110 if (pthread_mutex_lock(mutex) != PTHR_SUCCESS) {
111 VBX_print("ERROR: pthread_cond_wait can't reacquire mutex %x\n", mutex);
112 return PTHR_FAILURE;
113 }
114
115 return PTHR_SUCCESS;
116 }
117
118 Int
119 pthread_cond_signal(pthread_cond_t * cond)
120 /*---------------------------------------------------------------------------*
121 * DESCRIPTION
122 * Signal a condition variable
123 *---------------------------------------------------------------------------*/
124 {
125 Int tid, status;
126
127 if (!cond || cond->defunct)
128 return PTHR_FAILURE;
129
130 if (msgQNumMsgs(cond->waitersMsgQ) > 0) {
131 if (msgQReceive(cond->waitersMsgQ, (char *)&tid, sizeof(Int),
132 WAIT_FOREVER) == ERROR) {
133 VBX_print("ERROR: pthread_cond_signal can't read waiter tid\n");
134 return PTHR_FAILURE;
135 }
136 if (taskResume(tid) == ERROR) {
137 VBX_print("ERROR: pthread_cond_signal can't restart task %d\n", tid);
138 return PTHR_FAILURE;
139 }
140 }
141
142 return PTHR_SUCCESS;
143 }
144
145
2
3 /*---------------------------------------------------------------------------*
4 * DESCRIPTION
5 * Initialize a condition variable
6 *---------------------------------------------------------------------------*/
7 {
8 if (!cond)
9 return PTHR_FAILURE;
10
11 cond->defunct = TRUE;
12 if ((cond->waitersMsgQ = msgQCreate(PTHR_MAX_WAITERS, sizeof(Int),
13 MSG_Q_FIFO)) == NULL)
14 return PTHR_FAILURE;
15 cond->defunct = FALSE;
16 return PTHR_SUCCESS;
17 }
18
19 Int
20 pthread_cond_destroy(pthread_cond_t * cond)
21 /*---------------------------------------------------------------------------*
22 * DESCRIPTION
23 * Destroy a condition variable
24 *---------------------------------------------------------------------------*/
25 {
26 Int tid;
27
28 if (!cond)
29 return PTHR_FAILURE;
30
31 cond->defunct = TRUE;
32 while (msgQNumMsgs(cond->waitersMsgQ) > 0) {
33 if (msgQReceive(cond->waitersMsgQ, (char *)&tid, sizeof(Int),
34 WAIT_FOREVER) == ERROR) {
35 VBX_print("ERROR: pthread_cond_signal can't read waiter tid\n");
36 return PTHR_FAILURE;
37 }
38 if (taskResume(tid) == ERROR) {
39 VBX_print("ERROR: pthread_cond_signal can't restart task %d\n", tid);
40 return PTHR_FAILURE;
41 }
42 }
43
44 if (msgQDelete(cond->waitersMsgQ) == ERROR) {
45 VBX_print("ERROR: pthread_cond_destroy can't delete msgQ!\n");
46 return PTHR_FAILURE;
47 }
48
49 return PTHR_SUCCESS;
50 }
51
52 Int
53 pthread_cond_wait(pthread_cond_t * cond , pthread_mutex_t * mutex)
54 /*---------------------------------------------------------------------------*
55 * DESCRIPTION
56 * Wait on a condition variable
57 *---------------------------------------------------------------------------*/
58 {
59 Int tid, status;
60
61 if (!cond || !mutex || cond->defunct)
62 return PTHR_FAILURE;
63
64 tid = taskIdSelf();
65
66 if (msgQSend(cond->waitersMsgQ, (char *) &tid, sizeof(Int),
67 WAIT_FOREVER, MSG_PRI_NORMAL) == ERROR) {
68 VBX_print("ERROR: pthread_cond_wait can't place taskId on waiters msgQ\n");
69 return PTHR_FAILURE;
70 }
71
72 if (pthread_mutex_unlock(mutex) != PTHR_SUCCESS) {
73 VBX_print("ERROR: pthread_cond_wait can't unlock mutex %x\n", mutex);
74 return PTHR_FAILURE;
75 }
76
77 if ((status = taskSuspend(tid)) != OK) {
78 VBX_print("ERROR: pthread_cond_wait can't suspend task 5d\n", tid);
79 return PTHR_FAILURE;
80 }
81
82 if (pthread_mutex_lock(mutex) != PTHR_SUCCESS) {
83 VBX_print("ERROR: pthread_cond_wait can't reacquire mutex %x\n", mutex);
84 return PTHR_FAILURE;
85 }
86
87 return PTHR_SUCCESS;
88 }
89
90 Int
91 pthread_cond_timedwait(pthread_cond_t * cond , pthread_mutex_t * mutex,
92 const struct timespec * abstime)
93 /*---------------------------------------------------------------------------*
94 * DESCRIPTION
95 * Cop-out version of a timed condition variable wait
96 *---------------------------------------------------------------------------*/
97 {
98 Int tid, status;
99
100 if (!cond || !mutex || !abstime)
101 return PTHR_FAILURE;
102
103 if (pthread_mutex_unlock(mutex) != PTHR_SUCCESS) {
104 VBX_print("ERROR: pthread_cond_wait can't unlock mutex %x\n", mutex);
105 return PTHR_FAILURE;
106 }
107
108 pthread_delay_np(abstime);
109
110 if (pthread_mutex_lock(mutex) != PTHR_SUCCESS) {
111 VBX_print("ERROR: pthread_cond_wait can't reacquire mutex %x\n", mutex);
112 return PTHR_FAILURE;
113 }
114
115 return PTHR_SUCCESS;
116 }
117
118 Int
119 pthread_cond_signal(pthread_cond_t * cond)
120 /*---------------------------------------------------------------------------*
121 * DESCRIPTION
122 * Signal a condition variable
123 *---------------------------------------------------------------------------*/
124 {
125 Int tid, status;
126
127 if (!cond || cond->defunct)
128 return PTHR_FAILURE;
129
130 if (msgQNumMsgs(cond->waitersMsgQ) > 0) {
131 if (msgQReceive(cond->waitersMsgQ, (char *)&tid, sizeof(Int),
132 WAIT_FOREVER) == ERROR) {
133 VBX_print("ERROR: pthread_cond_signal can't read waiter tid\n");
134 return PTHR_FAILURE;
135 }
136 if (taskResume(tid) == ERROR) {
137 VBX_print("ERROR: pthread_cond_signal can't restart task %d\n", tid);
138 return PTHR_FAILURE;
139 }
140 }
141
142 return PTHR_SUCCESS;
143 }
144
145