on master help sign in
Add a timeout to checkMessages
3760508Edwin Brady
libs/base/System/Concurrency/Raw.idr+9
@@ -19,6 +19,15 @@ checkMsgs = do msgs <- foreign FFI_C "idris_checkMessages" (Ptr -> IO Ptr)
19 null <- nullPtr msgs
19 null <- nullPtr msgs
20 return (not null)
20 return (not null)
21
21
22+||| Check for messages in the process inbox
23+||| If no messages, waits for the given number of seconds
24+checkMsgsTimeout : Int -> IO Bool
25+checkMsgsTimeout timeout
26+ = do msgs <- foreign FFI_C "idris_checkMessagesTimeout"
27+ (Ptr -> Int -> IO Ptr) prim__vm timeout
28+ null <- nullPtr msgs
29+ return (not null)
30+
22||| Check for messages in the process inbox.
31||| Check for messages in the process inbox.
23||| Returns either 'Nothing', if none, or 'Just pid' as pid of sender.
32||| Returns either 'Nothing', if none, or 'Just pid' as pid of sender.
24listenMsgs : IO (Maybe Ptr)
33listenMsgs : IO (Maybe Ptr)
rts/idris_rts.c+23
@@ -801,6 +801,29 @@ VM* idris_checkMessagesFrom(VM* vm, VM* sender) {
801 return 0;
801 return 0;
802}
802}
803
803
804+VM* idris_checkMessagesTimeout(VM* vm, int delay) {
805+ VM* sender = idris_checkMessagesFrom(vm, NULL);
806+ if (sender != NULL) {
807+ return sender;
808+ }
809+
810+ struct timespec timeout;
811+ int status;
812+
813+ // Wait either for a timeout or until we get a signal that a message
814+ // has arrived.
815+ pthread_mutex_lock(&vm->inbox_block);
816+ timeout.tv_sec = time (NULL) + delay;
817+ timeout.tv_nsec = 0;
818+ status = pthread_cond_timedwait(&vm->inbox_waiting, &vm->inbox_block,
819+ &timeout);
820+ (void)(status); //don't emit 'unused' warning
821+ pthread_mutex_unlock(&vm->inbox_block);
822+
823+ return idris_checkMessagesFrom(vm, NULL);
824+}
825+
826+
804Msg* idris_getMessageFrom(VM* vm, VM* sender) {
827Msg* idris_getMessageFrom(VM* vm, VM* sender) {
805 Msg* msg;
828 Msg* msg;
806
829
rts/idris_rts.h+2
@@ -259,6 +259,8 @@ void idris_sendMessage(VM* sender, VM* dest, VAL msg);
259VM* idris_checkMessages(VM* vm);
259VM* idris_checkMessages(VM* vm);
260// Check whether there are any messages in the queue
260// Check whether there are any messages in the queue
261VM* idris_checkMessagesFrom(VM* vm, VM* sender);
261VM* idris_checkMessagesFrom(VM* vm, VM* sender);
262+// Check whether there are any messages in the queue, and wait if not
263+VM* idris_checkMessagesTimeout(VM* vm, int timeout);
262// block until there is a message in the queue
264// block until there is a message in the queue
263Msg* idris_recvMessage(VM* vm);
265Msg* idris_recvMessage(VM* vm);
264// block until there is a message in the queue
266// block until there is a message in the queue