+
+void
+rts_lock()
+{
+#ifdef RTS_SUPPORTS_THREADS
+ ACQUIRE_LOCK(&sched_mutex);
+
+ // we request to get the capability immediately, in order to
+ // a) stop other threads from using allocate()
+ // b) wake the current worker thread from awaitEvent()
+ // (so that a thread started by rts_eval* will start immediately)
+ grabReturnCapability(&sched_mutex,&rtsApiCapability);
+
+ // In the RTS hasn't been entered yet,
+ // start a RTS task.
+ // If there is already a task available (waiting for the work capability),
+ // this will do nothing.
+ startSchedulerTask();
+#endif
+}
+
+void
+rts_unlock()
+{
+#ifdef RTS_SUPPORTS_THREADS
+ rtsApiCapability = NULL;
+ RELEASE_LOCK(&sched_mutex);
+#endif
+}