1 /* 2 * Copyright 2022, Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT license. 4 */ 5 6 #include <compat/sys/mutex.h> 7 #include <compat/sys/condvar.h> 8 #include <compat/sys/kernel.h> 9 10 11 void 12 cv_init(struct cv* variable, const char* description) 13 { 14 variable->condition.Init(NULL, description); 15 } 16 17 18 void 19 cv_signal(struct cv* variable) 20 { 21 variable->condition.NotifyOne(); 22 } 23 24 25 int 26 cv_timedwait(struct cv* variable, struct mtx* mutex, int timeout) 27 { 28 int status; 29 30 const uint32 flags = timeout ? B_RELATIVE_TIMEOUT : 0; 31 const bigtime_t bigtimeout = TICKS_2_USEC(timeout); 32 33 if (mutex->type == MTX_RECURSE) { 34 // Special case: let the ConditionVariable handle switching recursive locks. 35 status = variable->condition.Wait(&mutex->u.recursive, 36 flags, bigtimeout); 37 return status; 38 } 39 40 ConditionVariableEntry entry; 41 variable->condition.Add(&entry); 42 43 mtx_unlock(mutex); 44 45 status = entry.Wait(flags, bigtimeout); 46 47 mtx_lock(mutex); 48 return status; 49 } 50 51 52 void 53 cv_wait(struct cv* variable, struct mtx* mutex) 54 { 55 cv_timedwait(variable, mutex, 0); 56 } 57