comparison src/thread/pthread/SDL_syscond.c @ 1668:4da1ee79c9af SDL-1.3

more tweaking indent options
author Sam Lantinga <slouken@libsdl.org>
date Mon, 29 May 2006 04:04:35 +0000
parents 782fd950bd46
children
comparison
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
34 pthread_cond_t cond; 34 pthread_cond_t cond;
35 }; 35 };
36 36
37 /* Create a condition variable */ 37 /* Create a condition variable */
38 SDL_cond * 38 SDL_cond *
39 SDL_CreateCond (void) 39 SDL_CreateCond(void)
40 { 40 {
41 SDL_cond *cond; 41 SDL_cond *cond;
42 42
43 cond = (SDL_cond *) SDL_malloc (sizeof (SDL_cond)); 43 cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
44 if (cond) { 44 if (cond) {
45 if (pthread_cond_init (&cond->cond, NULL) < 0) { 45 if (pthread_cond_init(&cond->cond, NULL) < 0) {
46 SDL_SetError ("pthread_cond_init() failed"); 46 SDL_SetError("pthread_cond_init() failed");
47 SDL_free (cond); 47 SDL_free(cond);
48 cond = NULL; 48 cond = NULL;
49 } 49 }
50 } 50 }
51 return (cond); 51 return (cond);
52 } 52 }
53 53
54 /* Destroy a condition variable */ 54 /* Destroy a condition variable */
55 void 55 void
56 SDL_DestroyCond (SDL_cond * cond) 56 SDL_DestroyCond(SDL_cond * cond)
57 { 57 {
58 if (cond) { 58 if (cond) {
59 pthread_cond_destroy (&cond->cond); 59 pthread_cond_destroy(&cond->cond);
60 SDL_free (cond); 60 SDL_free(cond);
61 } 61 }
62 } 62 }
63 63
64 /* Restart one of the threads that are waiting on the condition variable */ 64 /* Restart one of the threads that are waiting on the condition variable */
65 int 65 int
66 SDL_CondSignal (SDL_cond * cond) 66 SDL_CondSignal(SDL_cond * cond)
67 { 67 {
68 int retval; 68 int retval;
69 69
70 if (!cond) { 70 if (!cond) {
71 SDL_SetError ("Passed a NULL condition variable"); 71 SDL_SetError("Passed a NULL condition variable");
72 return -1; 72 return -1;
73 } 73 }
74 74
75 retval = 0; 75 retval = 0;
76 if (pthread_cond_signal (&cond->cond) != 0) { 76 if (pthread_cond_signal(&cond->cond) != 0) {
77 SDL_SetError ("pthread_cond_signal() failed"); 77 SDL_SetError("pthread_cond_signal() failed");
78 retval = -1; 78 retval = -1;
79 } 79 }
80 return retval; 80 return retval;
81 } 81 }
82 82
83 /* Restart all threads that are waiting on the condition variable */ 83 /* Restart all threads that are waiting on the condition variable */
84 int 84 int
85 SDL_CondBroadcast (SDL_cond * cond) 85 SDL_CondBroadcast(SDL_cond * cond)
86 { 86 {
87 int retval; 87 int retval;
88 88
89 if (!cond) { 89 if (!cond) {
90 SDL_SetError ("Passed a NULL condition variable"); 90 SDL_SetError("Passed a NULL condition variable");
91 return -1; 91 return -1;
92 } 92 }
93 93
94 retval = 0; 94 retval = 0;
95 if (pthread_cond_broadcast (&cond->cond) != 0) { 95 if (pthread_cond_broadcast(&cond->cond) != 0) {
96 SDL_SetError ("pthread_cond_broadcast() failed"); 96 SDL_SetError("pthread_cond_broadcast() failed");
97 retval = -1; 97 retval = -1;
98 } 98 }
99 return retval; 99 return retval;
100 } 100 }
101 101
102 int 102 int
103 SDL_CondWaitTimeout (SDL_cond * cond, SDL_mutex * mutex, Uint32 ms) 103 SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
104 { 104 {
105 int retval; 105 int retval;
106 struct timeval delta; 106 struct timeval delta;
107 struct timespec abstime; 107 struct timespec abstime;
108 108
109 if (!cond) { 109 if (!cond) {
110 SDL_SetError ("Passed a NULL condition variable"); 110 SDL_SetError("Passed a NULL condition variable");
111 return -1; 111 return -1;
112 } 112 }
113 113
114 gettimeofday (&delta, NULL); 114 gettimeofday(&delta, NULL);
115 115
116 abstime.tv_sec = delta.tv_sec + (ms / 1000); 116 abstime.tv_sec = delta.tv_sec + (ms / 1000);
117 abstime.tv_nsec = (delta.tv_usec + (ms % 1000) * 1000) * 1000; 117 abstime.tv_nsec = (delta.tv_usec + (ms % 1000) * 1000) * 1000;
118 if (abstime.tv_nsec > 1000000000) { 118 if (abstime.tv_nsec > 1000000000) {
119 abstime.tv_sec += 1; 119 abstime.tv_sec += 1;
120 abstime.tv_nsec -= 1000000000; 120 abstime.tv_nsec -= 1000000000;
121 } 121 }
122 122
123 tryagain: 123 tryagain:
124 retval = pthread_cond_timedwait (&cond->cond, &mutex->id, &abstime); 124 retval = pthread_cond_timedwait(&cond->cond, &mutex->id, &abstime);
125 switch (retval) { 125 switch (retval) {
126 case EINTR: 126 case EINTR:
127 goto tryagain; 127 goto tryagain;
128 break; 128 break;
129 case ETIMEDOUT: 129 case ETIMEDOUT:
130 retval = SDL_MUTEX_TIMEDOUT; 130 retval = SDL_MUTEX_TIMEDOUT;
131 break; 131 break;
132 case 0: 132 case 0:
133 break; 133 break;
134 default: 134 default:
135 SDL_SetError ("pthread_cond_timedwait() failed"); 135 SDL_SetError("pthread_cond_timedwait() failed");
136 retval = -1; 136 retval = -1;
137 break; 137 break;
138 } 138 }
139 return retval; 139 return retval;
140 } 140 }
141 141
142 /* Wait on the condition variable, unlocking the provided mutex. 142 /* Wait on the condition variable, unlocking the provided mutex.
143 The mutex must be locked before entering this function! 143 The mutex must be locked before entering this function!
144 */ 144 */
145 int 145 int
146 SDL_CondWait (SDL_cond * cond, SDL_mutex * mutex) 146 SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex)
147 { 147 {
148 int retval; 148 int retval;
149 149
150 if (!cond) { 150 if (!cond) {
151 SDL_SetError ("Passed a NULL condition variable"); 151 SDL_SetError("Passed a NULL condition variable");
152 return -1; 152 return -1;
153 } 153 }
154 154
155 retval = 0; 155 retval = 0;
156 if (pthread_cond_wait (&cond->cond, &mutex->id) != 0) { 156 if (pthread_cond_wait(&cond->cond, &mutex->id) != 0) {
157 SDL_SetError ("pthread_cond_wait() failed"); 157 SDL_SetError("pthread_cond_wait() failed");
158 retval = -1; 158 retval = -1;
159 } 159 }
160 return retval; 160 return retval;
161 } 161 }
162 162