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 timermp
.mp_Node
.ln_Type
= NT_MSGPORT
;
53 timermp
.mp_Node
.ln_Pri
= 0;
54 timermp
.mp_Node
.ln_Name
= NULL
;
55 timermp
.mp_Flags
= PA_SIGNAL
;
56 timermp
.mp_SigTask
= task
;
57 signal
= AllocSignal(-1);
60 signal
= SIGB_TIMER_FALLBACK
;
61 SetSignal(SIGF_TIMER_FALLBACK
, 0);
63 timermp
.mp_SigBit
= signal
;
64 NEWLIST(&timermp
.mp_MsgList
);
67 timerio
.tr_node
.io_Message
.mn_Node
.ln_Type
= NT_MESSAGE
;
68 timerio
.tr_node
.io_Message
.mn_Node
.ln_Pri
= 0;
69 timerio
.tr_node
.io_Message
.mn_Node
.ln_Name
= NULL
;
70 timerio
.tr_node
.io_Message
.mn_ReplyPort
= &timermp
;
71 timerio
.tr_node
.io_Message
.mn_Length
= sizeof(struct timerequest
);
74 if (OpenDevice((STRPTR
)TIMERNAME
, UNIT_MICROHZ
, &timerio
.tr_node
, 0) != 0)
76 if (timermp
.mp_SigBit
!= SIGB_TIMER_FALLBACK
)
77 FreeSignal(timermp
.mp_SigBit
);
82 // prepare the device command and send it
83 timerio
.tr_node
.io_Command
= TR_ADDREQUEST
;
84 timerio
.tr_node
.io_Flags
= 0;
85 TIMESPEC_TO_TIMEVAL(&timerio
.tr_time
, abstime
);
88 struct timeval starttime
;
89 // absolute time has to be converted to relative
90 // GetSysTime can't be used due to the timezone offset in abstime
91 gettimeofday(&starttime
, NULL
);
92 timersub(&timerio
.tr_time
, &starttime
, &timerio
.tr_time
);
94 timermask
= 1 << timermp
.mp_SigBit
;
96 SendIO((struct IORequest
*)&timerio
);
99 // prepare a waiter node
101 signal
= AllocSignal(-1);
104 signal
= SIGB_COND_FALLBACK
;
105 SetSignal(SIGF_COND_FALLBACK
, 0);
107 waiter
.sigmask
= 1 << signal
;
108 sigs
|= waiter
.sigmask
;
110 // add it to the end of the list
111 ObtainSemaphore(&cond
->semaphore
);
112 AddTail((struct List
*)&cond
->waiters
, (struct Node
*)&waiter
);
113 ReleaseSemaphore(&cond
->semaphore
);
115 // wait for the condition to be signalled or the timeout
117 pthread_mutex_unlock(mutex
);
119 pthread_mutex_lock(mutex
);
122 // remove the node from the list
123 ObtainSemaphore(&cond
->semaphore
);
124 Remove((struct Node
*)&waiter
);
125 ReleaseSemaphore(&cond
->semaphore
);
127 if (signal
!= SIGB_COND_FALLBACK
)
132 // clean up the timerequest
133 if (!CheckIO((struct IORequest
*)&timerio
))
135 AbortIO((struct IORequest
*)&timerio
);
136 WaitIO((struct IORequest
*)&timerio
);
138 CloseDevice((struct IORequest
*)&timerio
);
140 if (timermp
.mp_SigBit
!= SIGB_TIMER_FALLBACK
)
141 FreeSignal(timermp
.mp_SigBit
);
144 if (sigs
& timermask
)
151 int pthread_cond_timedwait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
, const struct timespec
*abstime
)
153 D(bug("%s(%p, %p, %p)\n", __FUNCTION__
, cond
, mutex
, abstime
));
155 return _pthread_cond_timedwait(cond
, mutex
, abstime
, FALSE
);