MusicKit
0.0.0
|
00001 /* 00002 $Id$ 00003 00004 Description: 00005 An implementation of condition locks intended to replace the 00006 standard NSConditionLock which under Windows does not properly 00007 implement conditions. 00008 00009 This win32 condition code is substantially taken from 00010 http://www.cs.wustl.edu/~schmidt/win32-cv-1.html 00011 00012 Original Author: Stephen Brandon, <stephen@brandonitconsulting.co.uk> 00013 00014 Copyright (c) 2001, The MusicKit Project. All rights reserved. 00015 00016 Permission is granted to use and modify this code for commercial and non-commercial 00017 purposes so long as the author attribution and copyright messages remain intact and 00018 accompany all relevant code. 00019 */ 00020 00021 #ifndef __SNDCONDITIONLOCK_H__ 00022 #define __SNDCONDITIONLOCK_H__ 00023 00024 #ifdef __MINGW32__ 00025 00026 #import <windows.h> 00027 #import <Foundation/Foundation.h> 00028 00029 00030 typedef struct 00031 { 00032 int waiters_count_; 00033 // Number of waiting threads. 00034 00035 CRITICAL_SECTION waiters_count_lock_; 00036 // Serialize access to <waiters_count_>. 00037 00038 HANDLE sema_; 00039 // Semaphore used to queue up threads waiting for the condition to 00040 // become signaled. 00041 00042 HANDLE waiters_done_; 00043 // An auto-reset event used by the broadcast/signal thread to wait 00044 // for all the waiting thread(s) to wake up and be released from the 00045 // semaphore. 00046 00047 size_t was_broadcast_; 00048 // Keeps track of whether we were broadcasting or signaling. This 00049 // allows us to optimize the code if we're just signaling. 00050 } pthread_cond_t; 00051 00052 typedef HANDLE pthread_mutex_t; 00053 00054 00055 00065 @interface SndConditionLock : NSObject <NSLocking, GCFinalization> 00066 { 00067 @private 00068 objc_condition_t _condition; 00069 objc_mutex_t _mutex; 00070 int _condition_value; 00071 } 00072 00073 /* 00074 * this was originally part of the NSLocking protocol 00075 */ 00076 - (void) lock; 00077 - (void) unlock; 00078 00079 /* 00080 * Initialize lock with condition 00081 */ 00082 - (id) initWithCondition: (int)value; 00083 00084 /* 00085 * Return the current condition of the lock 00086 */ 00087 - (int) condition; 00088 00089 /* 00090 * Acquiring and release the lock 00091 */ 00092 - (void) lockWhenCondition: (int)value; 00093 - (void) unlockWithCondition: (int)value; 00094 - (BOOL) tryLock; 00095 - (BOOL) tryLockWhenCondition: (int)value; 00096 00097 /* 00098 * Acquiring the lock with a date condition 00099 */ 00100 - (BOOL) lockBeforeDate: (NSDate*)limit; 00101 - (BOOL) lockWhenCondition: (int)condition 00102 beforeDate: (NSDate*)limit; 00103 00104 - (void) lock; 00105 - (void) unlock; 00106 00107 @end 00108 00109 #endif /* mingw32 */ 00110 00111 #endif