2 Copyright (C) 2014 Szilard Biro
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
21 #include <proto/timer.h>
23 #include "pthread_intern.h"
26 int _pthread_cond_timedwait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
, const struct timespec
*abstime
, BOOL relative
)
32 struct MsgPort timermp
;
33 struct timerequest timerio
;
36 DB2(bug("%s(%p, %p, %p)\n", __FUNCTION__
, cond
, mutex
, abstime
));
38 if (cond
== NULL
|| mutex
== NULL
)
43 // initialize static conditions
44 if (SemaphoreIsInvalid(&cond
->semaphore
))
45 pthread_cond_init(cond
, NULL
);
47 task
= FindTask(NULL
);
52 memset( &timermp
, 0, sizeof( timermp
) );
53 timermp
.mp_Node
.ln_Type
= NT_MSGPORT
;
54 timermp
.mp_Flags
= PA_SIGNAL
;
55 timermp
.mp_SigTask
= task
;
56 signal
= AllocSignal(-1);
59 signal
= SIGB_TIMER_FALLBACK
;
60 SetSignal(SIGF_TIMER_FALLBACK
, 0);
62 timermp
.mp_SigBit
= signal
;
63 NEWLIST(&timermp
.mp_MsgList
);
66 timerio
.tr_node
.io_Message
.mn_Node
.ln_Type
= NT_MESSAGE
;
67 timerio
.tr_node
.io_Message
.mn_Node
.ln_Pri
= 0;
68 timerio
.tr_node
.io_Message
.mn_Node
.ln_Name
= NULL
;
69 timerio
.tr_node
.io_Message
.mn_ReplyPort
= &timermp
;
70 timerio
.tr_node
.io_Message
.mn_Length
= sizeof(struct timerequest
);
73 if (OpenDevice((STRPTR
)TIMERNAME
, UNIT_MICROHZ
, &timerio
.tr_node
, 0) != 0)
75 if (timermp
.mp_SigBit
!= SIGB_TIMER_FALLBACK
)
76 FreeSignal(timermp
.mp_SigBit
);
81 // prepare the device command and send it
82 timerio
.tr_node
.io_Command
= TR_ADDREQUEST
;
83 timerio
.tr_node
.io_Flags
= 0;
84 TIMESPEC_TO_TIMEVAL(&timerio
.tr_time
, abstime
);
87 struct timeval starttime
;
88 // absolute time has to be converted to relative
89 // GetSysTime can't be used due to the timezone offset in abstime
90 gettimeofday(&starttime
, NULL
);
91 timersub(&timerio
.tr_time
, &starttime
, &timerio
.tr_time
);
93 timermask
= 1 << timermp
.mp_SigBit
;
95 SendIO((struct IORequest
*)&timerio
);
98 // prepare a waiter node
100 signal
= AllocSignal(-1);
103 signal
= SIGB_COND_FALLBACK
;
104 SetSignal(SIGF_COND_FALLBACK
, 0);
106 waiter
.sigmask
= 1 << signal
;
107 sigs
|= waiter
.sigmask
;
109 // add it to the end of the list
110 ObtainSemaphore(&cond
->semaphore
);
111 AddTail((struct List
*)&cond
->waiters
, (struct Node
*)&waiter
);
112 ReleaseSemaphore(&cond
->semaphore
);
114 // wait for the condition to be signalled or the timeout
116 pthread_mutex_unlock(mutex
);
118 pthread_mutex_lock(mutex
);
121 // remove the node from the list
122 ObtainSemaphore(&cond
->semaphore
);
123 Remove((struct Node
*)&waiter
);
124 ReleaseSemaphore(&cond
->semaphore
);
126 if (signal
!= SIGB_COND_FALLBACK
)
131 // clean up the timerequest
132 if (!CheckIO((struct IORequest
*)&timerio
))
134 AbortIO((struct IORequest
*)&timerio
);
135 WaitIO((struct IORequest
*)&timerio
);
137 CloseDevice((struct IORequest
*)&timerio
);
139 if (timermp
.mp_SigBit
!= SIGB_TIMER_FALLBACK
)
140 FreeSignal(timermp
.mp_SigBit
);
143 if (sigs
& timermask
)
150 int pthread_cond_timedwait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
, const struct timespec
*abstime
)
152 D(bug("%s(%p, %p, %p)\n", __FUNCTION__
, cond
, mutex
, abstime
));
154 return _pthread_cond_timedwait(cond
, mutex
, abstime
, FALSE
);