#include <time.h>
#include <stdbool.h>
#include <stdint.h>
+#include <signal.h>
#define PTHREAD_XTIME_NOBLOCK (&(struct timespec){ .tv_sec = 0, .tv_nsec = 0 })
#define PTHREAD_XTIME_FOREVER NULL
+//Pausing
+
+#define PTHREAD_XSIG_STOP (SIGRTMIN+0)
+#define PTHREAD_XSIG_CONT (SIGRTMIN+1)
+#define PTHREAD_XSIGRTMIN (SIGRTMIN+2) //First unused RT signal
+
+#define pthread_pause(t) (pthread_kill((t), PTHREAD_XSIG_STOP));
+#define pthread_unpause(t) (pthread_kill((t), PTHREAD_XSIG_CONT));
+
+void pthread_unpause_handler();
+void pthread_pause_handler();
+void pthread_pause_enable();
+
// Message queues
#define PTHREAD_XMQ_FRONT true
char str[128];
while(1) {
- pthread_mq_receive_generic(&myq, &str, false, PTHREAD_XTIME_FOREVER);
- printf("RECVD: %.6s\t\t(waiting %d)\n", str, (int)pthread_mq_waiting(&myq));
- sleep(1);
+ if(pthread_mq_receive_generic(&myq, &str, false, PTHREAD_XTIME_FOREVER))
+ printf("-RECVD: %.6s\t\t(waiting %d)\n", str, (int)pthread_mq_waiting(&myq));
+ if(pthread_mq_receive_generic(&myq, &str, false, PTHREAD_XTIME_NOBLOCK))
+ printf("+RECVD: %.6s\t\t(waiting %d)\n", str, (int)pthread_mq_waiting(&myq));
}
}
+void *thread_send() {
+ while(1) {
+ pthread_mq_send_generic(&myq, " X ", true, PTHREAD_XTIME_NOBLOCK);
+ pthread_mq_send_generic(&myq, " Y ", false, PTHREAD_XTIME_FOREVER);
+ }
+}
+
+
int main() {
//main_mumu();
pthread_t t;
pthread_create(&t, NULL, thread_recv, NULL);
+ pthread_t s;
+ pthread_create(&s, NULL, thread_send, NULL);
pthread_mq_send_generic(&myq, "AHOJ1", false, NULL);
pthread_mq_send_generic(&myq, "AHOJ2", false, NULL);
pthread_mq_send_generic(&myq, "AHOJ6", true, NULL);
while(1) {
- pthread_mq_send_generic(&myq, "B", false, NULL);
- pthread_mq_send_generic(&myq, "A", true, NULL);
- pthread_mq_send_generic(&myq, " A", false, NULL);
- pthread_mq_send_generic(&myq, " B", false, NULL);
- sleep(1);
+ pthread_mq_send_generic(&myq, "B ", false, NULL);
+ pthread_mq_send_generic(&myq, "A ", true, NULL);
+ pthread_mq_send_generic(&myq, " B ", false, PTHREAD_XTIME_FOREVER);
+ pthread_mq_send_generic(&myq, " A ", false, PTHREAD_XTIME_NOBLOCK);
}
pthread_join(t, NULL);
--- /dev/null
+#include <signal.h>
+#include <pthread.h>
+#include <pthread_extra.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <unistd.h>
+
+void *thread_test(void *arg) {
+ //Whole process dies if you kill thread immediately before it is pausable
+ //pthread_pause_enable();
+ while(1) {
+ usleep(1000*300);
+ printf("Running%s!\n", (char *)arg);
+ }
+}
+
+int main() {
+
+ pthread_t a, b;
+ pthread_pause_enable(); //Will get inherited by all threads from now on
+ //That way you can be sure it is pausable immediately
+ pthread_create(&a, NULL, thread_test, " A");
+ pthread_create(&b, NULL, thread_test, " B");
+
+ while(1) {
+ pthread_pause(b);
+ pthread_unpause(a);
+ printf("SWITCH A:\n");
+ sleep(2);
+
+ printf("SWITCH B:\n");
+ pthread_pause(a);
+ pthread_unpause(b);
+ sleep(2);
+
+ printf("SWITCH A+B:\n");
+ pthread_unpause(a);
+ pthread_unpause(b);
+ sleep(1);
+ }
+
+ pthread_join(a, NULL);
+ pthread_join(b, NULL);
+ printf("DIEDED!\n");
+}