Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/*
 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 * 
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this
 * file.
 * 
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 * 
 * @APPLE_LICENSE_HEADER_END@
 */
/*
 * Copyright 1996 1995 by Open Software Foundation, Inc. 1997 1996 1995 1994 1993 1992 1991  
 *              All Rights Reserved 
 *  
 * Permission to use, copy, modify, and distribute this software and 
 * its documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appears in all copies and 
 * that both the copyright notice and this permission notice appear in 
 * supporting documentation. 
 *  
 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE 
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 * FOR A PARTICULAR PURPOSE. 
 *  
 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR 
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT, 
 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 
 */
/*
 * MkLinux
 */

/*
 * POSIX Pthread Library
 */

#include "pthread_internals.h"
#include <sys/time.h>              /* For struct timespec and getclock(). */
#include <stdio.h>
    
extern void _pthread_mutex_remove(pthread_mutex_t *, pthread_t);
extern int __unix_conforming;

#ifndef BUILDING_VARIANT /* [ */

/*
 * Destroy a condition variable.
 */
int       
pthread_cond_destroy(pthread_cond_t *cond)
{
	int ret;
	int sig = cond->sig;

	/* to provide backwards compat for apps using united condtn vars */
	if((sig != _PTHREAD_COND_SIG) && (sig !=_PTHREAD_COND_SIG_init))
		return(EINVAL);

	LOCK(cond->lock);
	if (cond->sig == _PTHREAD_COND_SIG)
	{
		if (cond->busy == (pthread_mutex_t *)NULL)
		{
			cond->sig = _PTHREAD_NO_SIG;
			ret = ESUCCESS;
		} else
			ret = EBUSY;
	} else
		ret = EINVAL; /* Not an initialized condition variable structure */
	UNLOCK(cond->lock);
	return (ret);
}

/*
 * Initialize a condition variable.  Note: 'attr' is ignored.
 */
static int       
_pthread_cond_init(pthread_cond_t *cond,
		  const pthread_condattr_t *attr)
{
	cond->next = (pthread_cond_t *)NULL;
	cond->prev = (pthread_cond_t *)NULL;
	cond->busy = (pthread_mutex_t *)NULL;
	cond->waiters = 0;
	cond->sigspending = 0;
	cond->sem = SEMAPHORE_NULL;
	cond->sig = _PTHREAD_COND_SIG;
	return (ESUCCESS);
}

/*
 * Initialize a condition variable.  This is the public interface.
 * We can't trust the lock, so initialize it first before taking
 * it.
 */
int       
pthread_cond_init(pthread_cond_t *cond,
		  const pthread_condattr_t *attr)
{
	LOCK_INIT(cond->lock);
	return (_pthread_cond_init(cond, attr));
}

/*
 * Signal a condition variable, waking up all threads waiting for it.
 */
int       
pthread_cond_broadcast(pthread_cond_t *cond)
{
	kern_return_t kern_res;
	semaphore_t sem;
	int sig = cond->sig;

	/* to provide backwards compat for apps using united condtn vars */
	if((sig != _PTHREAD_COND_SIG) && (sig !=_PTHREAD_COND_SIG_init))
		return(EINVAL);

	LOCK(cond->lock);
	if (cond->sig != _PTHREAD_COND_SIG)
	{
		int res;

		if (cond->sig == _PTHREAD_COND_SIG_init)
		{
			_pthread_cond_init(cond, NULL);
			res = ESUCCESS;
		} else 
			res = EINVAL;  /* Not a condition variable */
		UNLOCK(cond->lock);
		return (res);
	}
	else if ((sem = cond->sem) == SEMAPHORE_NULL)
	{
		/* Avoid kernel call since there are no waiters... */
		UNLOCK(cond->lock);
		return (ESUCCESS);
	}
	cond->sigspending++;
	UNLOCK(cond->lock);

	PTHREAD_MACH_CALL(semaphore_signal_all(sem), kern_res);

	LOCK(cond->lock);
	cond->sigspending--;
	if (cond->waiters == 0 && cond->sigspending == 0)
	{
		cond->sem = SEMAPHORE_NULL;
		restore_sem_to_pool(sem);
	}
	UNLOCK(cond->lock);
	if (kern_res != KERN_SUCCESS)
		return (EINVAL);
	return (ESUCCESS);
}

/*
 * Signal a condition variable, waking a specified thread.
 */
int       
pthread_cond_signal_thread_np(pthread_cond_t *cond, pthread_t thread)
{
	kern_return_t kern_res;
	semaphore_t sem;
	int sig = cond->sig;

	/* to provide backwards compat for apps using united condtn vars */
	if((sig != _PTHREAD_COND_SIG) && (sig !=_PTHREAD_COND_SIG_init))
		return(EINVAL);

	LOCK(cond->lock);
	if (cond->sig != _PTHREAD_COND_SIG)
	{
		int ret;

		if (cond->sig == _PTHREAD_COND_SIG_init) 
		{
			_pthread_cond_init(cond, NULL);
			ret = ESUCCESS;
		}
		else
			ret = EINVAL; /* Not a condition variable */
		UNLOCK(cond->lock);
		return (ret);
	}
	else if ((sem = cond->sem) == SEMAPHORE_NULL)
	{
		/* Avoid kernel call since there are not enough waiters... */
		UNLOCK(cond->lock);
		return (ESUCCESS);
	}
	cond->sigspending++;
	UNLOCK(cond->lock);

	if (thread == (pthread_t)NULL)
	{
		kern_res = semaphore_signal_thread(sem, THREAD_NULL);
		if (kern_res == KERN_NOT_WAITING)
			kern_res = KERN_SUCCESS;
	}
	else if (thread->sig == _PTHREAD_SIG)
	{
	        PTHREAD_MACH_CALL(semaphore_signal_thread(
			sem, pthread_mach_thread_np(thread)), kern_res);
	}
	else
		kern_res = KERN_FAILURE;

	LOCK(cond->lock);
	cond->sigspending--;
	if (cond->waiters == 0 && cond->sigspending == 0)
	{
		cond->sem = SEMAPHORE_NULL;
		restore_sem_to_pool(sem);
	}
	UNLOCK(cond->lock);
	if (kern_res != KERN_SUCCESS)
		return (EINVAL);
	return (ESUCCESS);
}

/*
 * Signal a condition variable, waking only one thread.
 */
int
pthread_cond_signal(pthread_cond_t *cond)
{
	return pthread_cond_signal_thread_np(cond, NULL);
}

/*
 * Manage a list of condition variables associated with a mutex
 */

static void
_pthread_cond_add(pthread_cond_t *cond, pthread_mutex_t *mutex)
{
	pthread_cond_t *c;
	LOCK(mutex->lock);
	if ((c = mutex->busy) != (pthread_cond_t *)NULL)
	{
		c->prev = cond;
	} 
	cond->next = c;
	cond->prev = (pthread_cond_t *)NULL;
	mutex->busy = cond;
	UNLOCK(mutex->lock);
	if (cond->sem == SEMAPHORE_NULL)
		cond->sem = new_sem_from_pool();
}

static void
_pthread_cond_remove(pthread_cond_t *cond, pthread_mutex_t *mutex)
{
	pthread_cond_t *n, *p;

	LOCK(mutex->lock);
	if ((n = cond->next) != (pthread_cond_t *)NULL)
	{
		n->prev = cond->prev;
	}
	if ((p = cond->prev) != (pthread_cond_t *)NULL)
	{
		p->next = cond->next;
	} 
	else
	{ /* This is the first in the list */
		mutex->busy = n;
	}
	UNLOCK(mutex->lock);
	if (cond->sigspending == 0)
	{
		restore_sem_to_pool(cond->sem);
		cond->sem = SEMAPHORE_NULL;
	}
}

static void cond_cleanup(void *arg)
{
    pthread_cond_t *cond = (pthread_cond_t *)arg;
    pthread_mutex_t *mutex;
    LOCK(cond->lock);
    mutex = cond->busy;
    cond->waiters--;
    if (cond->waiters == 0) {
        _pthread_cond_remove(cond, mutex);
        cond->busy = (pthread_mutex_t *)NULL;
    }
    UNLOCK(cond->lock);
    /*
    ** Can't do anything if this fails -- we're on the way out
    */
    (void)pthread_mutex_lock(mutex);
}

/*
 * Suspend waiting for a condition variable.
 * Note: we have to keep a list of condition variables which are using
 * this same mutex variable so we can detect invalid 'destroy' sequences.
 */
__private_extern__ int       
_pthread_cond_wait(pthread_cond_t *cond, 
		   pthread_mutex_t *mutex,
		   const struct timespec *abstime,
		   int isRelative,
		    int isconforming)
{
	int res;
	kern_return_t kern_res;
	int wait_res;
	pthread_mutex_t *busy;
	mach_timespec_t then;
	struct timespec cthen = {0,0};
	int sig = cond->sig;

	/* to provide backwards compat for apps using united condtn vars */
	if((sig != _PTHREAD_COND_SIG) && (sig !=_PTHREAD_COND_SIG_init))
		return(EINVAL);
	LOCK(cond->lock);
	if (cond->sig != _PTHREAD_COND_SIG)
	{
		if (cond->sig != _PTHREAD_COND_SIG_init)
		{
			UNLOCK(cond->lock);
			return (EINVAL);        /* Not a condition variable */
		}
		_pthread_cond_init(cond, NULL);
	}

	if (abstime) {
		if (!isconforming)
		{
			if (isRelative == 0) {
				struct timespec now;
				struct timeval tv;
				gettimeofday(&tv, NULL);
				TIMEVAL_TO_TIMESPEC(&tv, &now);

				/* Compute relative time to sleep */
				then.tv_nsec = abstime->tv_nsec - now.tv_nsec;
				then.tv_sec = abstime->tv_sec - now.tv_sec;
				if (then.tv_nsec < 0)
				{
					then.tv_nsec += NSEC_PER_SEC;
					then.tv_sec--;
				}
				if (((int)then.tv_sec < 0) ||
					((then.tv_sec == 0) && (then.tv_nsec == 0)))
				{
					UNLOCK(cond->lock);
					return ETIMEDOUT;
				}
			} else {
				then.tv_sec = abstime->tv_sec;
				then.tv_nsec = abstime->tv_nsec;
			}
			if (then.tv_nsec >= NSEC_PER_SEC) {
				UNLOCK(cond->lock);
				return EINVAL;
			}
		} else {
			cthen.tv_sec = abstime->tv_sec;
            cthen.tv_nsec = abstime->tv_nsec;
            if ((cthen.tv_sec < 0) || (cthen.tv_nsec < 0)) {
                UNLOCK(cond->lock);
                return EINVAL;
            }
            if (cthen.tv_nsec >= NSEC_PER_SEC) {
                UNLOCK(cond->lock);
                return EINVAL;
            }
        }
	}

	if (++cond->waiters == 1)
	{
		_pthread_cond_add(cond, mutex);
		cond->busy = mutex;
	}
	else if ((busy = cond->busy) != mutex)
	{
		/* Must always specify the same mutex! */
		cond->waiters--;
		UNLOCK(cond->lock);
		return (EINVAL);
	}
	UNLOCK(cond->lock);
	
#if defined(DEBUG)
	_pthread_mutex_remove(mutex, pthread_self());
#endif
	LOCK(mutex->lock);
	if (--mutex->lock_count == 0)
	{
		if (mutex->sem == SEMAPHORE_NULL)
			mutex->sem = new_sem_from_pool();
		mutex->owner = _PTHREAD_MUTEX_OWNER_SWITCHING;
		UNLOCK(mutex->lock);

		if (!isconforming) {
			if (abstime) {
				kern_res = semaphore_timedwait_signal(cond->sem, mutex->sem, then);
			} else {
				PTHREAD_MACH_CALL(semaphore_wait_signal(cond->sem, mutex->sem), kern_res);
			}
		} else {
            pthread_cleanup_push(cond_cleanup, (void *)cond);
            wait_res = __semwait_signal(cond->sem, mutex->sem, abstime != NULL, isRelative,
			cthen.tv_sec, cthen.tv_nsec);
            pthread_cleanup_pop(0);
		}
	} else {
		UNLOCK(mutex->lock);
		if (!isconforming) {
			if (abstime) {
				kern_res = semaphore_timedwait(cond->sem, then);
			} else {
				PTHREAD_MACH_CALL(semaphore_wait(cond->sem), kern_res);
			}
		 } else {
				pthread_cleanup_push(cond_cleanup, (void *)cond);
                wait_res = __semwait_signal(cond->sem, NULL, abstime != NULL, isRelative,
			cthen.tv_sec, cthen.tv_nsec);
                pthread_cleanup_pop(0);
		}

	}

	LOCK(cond->lock);
	cond->waiters--;
	if (cond->waiters == 0)
	{
		_pthread_cond_remove(cond, mutex);
		cond->busy = (pthread_mutex_t *)NULL;
	}
	UNLOCK(cond->lock);
	if ((res = pthread_mutex_lock(mutex)) != ESUCCESS)
		return (res);

	if (!isconforming) {
		/* KERN_ABORTED can be treated as a spurious wakeup */
		if ((kern_res == KERN_SUCCESS) || (kern_res == KERN_ABORTED))
			return (ESUCCESS);
		else if (kern_res == KERN_OPERATION_TIMED_OUT)
			return (ETIMEDOUT);
		return (EINVAL);
	} else {
    	if (wait_res < 0) {
			if (errno == ETIMEDOUT) {
				return ETIMEDOUT;
			} else if (errno == EINTR) {
				/*
				**  EINTR can be treated as a spurious wakeup unless we were canceled.
				*/
				return 0;	
				}
			return EINVAL;
    	}
    	return 0;
	}
}


int       
pthread_cond_timedwait_relative_np(pthread_cond_t *cond, 
		       pthread_mutex_t *mutex,
		       const struct timespec *abstime)
{
	return (_pthread_cond_wait(cond, mutex, abstime, 1, 0));
}

int
pthread_condattr_init(pthread_condattr_t *attr)
{
        attr->sig = _PTHREAD_COND_ATTR_SIG;
        return (ESUCCESS);
}

int       
pthread_condattr_destroy(pthread_condattr_t *attr)
{
        attr->sig = _PTHREAD_NO_SIG;  /* Uninitialized */
        return (ESUCCESS);
}

int
pthread_condattr_getpshared(const pthread_condattr_t *attr,
				int *pshared)
{
        if (attr->sig == _PTHREAD_COND_ATTR_SIG)
        {
                *pshared = (int)PTHREAD_PROCESS_PRIVATE;
                return (ESUCCESS);
        } else
        {
                return (EINVAL); /* Not an initialized 'attribute' structure */
        }
}


int
pthread_condattr_setpshared(pthread_condattr_t * attr, int pshared)
{
        if (attr->sig == _PTHREAD_COND_ATTR_SIG)
        {
                if ( pshared == PTHREAD_PROCESS_PRIVATE)
                {
			/* attr->pshared = pshared */
                        return (ESUCCESS);
                } else
                {
                        return (EINVAL); /* Invalid parameter */
                }
        } else
        {
                return (EINVAL); /* Not an initialized 'attribute' structure */
        }

}

#else /* !BUILDING_VARIANT */
extern int _pthread_cond_wait(pthread_cond_t *cond, 
			pthread_mutex_t *mutex,
			const struct timespec *abstime,
			int isRelative,
			int isconforming);

#endif /* !BUILDING_VARIANT ] */

int       
pthread_cond_wait(pthread_cond_t *cond, 
		  pthread_mutex_t *mutex)
{
	int conforming;
#if __DARWIN_UNIX03

	if (__unix_conforming == 0)
		__unix_conforming = 1;

	conforming = 1;
#else /* __DARWIN_UNIX03 */
	conforming = 0;
#endif /* __DARWIN_UNIX03 */
	return (_pthread_cond_wait(cond, mutex, (struct timespec *)NULL, 0, conforming));
}

int       
pthread_cond_timedwait(pthread_cond_t *cond, 
		       pthread_mutex_t *mutex,
		       const struct timespec *abstime)
{
	int conforming;
#if __DARWIN_UNIX03
	if (__unix_conforming == 0)
		__unix_conforming = 1;

        conforming = 1;
#else /* __DARWIN_UNIX03 */
        conforming = 0;
#endif /* __DARWIN_UNIX03 */

	return (_pthread_cond_wait(cond, mutex, abstime, 0, conforming));
}