aboutsummaryrefslogtreecommitdiff
path: root/devel/libpdel/files/patch-util_pevent.c
blob: 1d50ab903e3c31ec472f548b311012ea1cfa99f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
--- util/pevent.c.orig	2009-05-13 21:36:05 UTC
+++ util/pevent.c
@@ -147,6 +147,15 @@ struct pevent {
 		}							\
 	} while (0)
 
+#define PEVENT_SET_OCCURRED(ctx, ev)					\
+	do {								\
+		(ev)->flags |= PEVENT_OCCURRED;				\
+		if ((ev) != TAILQ_FIRST(&ctx->events)) {		\
+			TAILQ_REMOVE(&(ctx)->events, (ev), next);	\
+			TAILQ_INSERT_HEAD(&(ctx)->events, (ev), next);	\
+		}							\
+	} while (0)
+
 /* Internal functions */
 static void	pevent_ctx_service(struct pevent *ev);
 static void	*pevent_ctx_main(void *arg);
@@ -336,7 +345,11 @@ pevent_register(struct pevent_ctx *ctx, struct pevent 
 			ev->u.millis = 0;
 		pd_gettimeofday(&ev->when, NULL);
 		ev->when.tv_sec += ev->u.millis / 1000;
-		ev->when.tv_usec += ev->u.millis % 1000;
+		ev->when.tv_usec += (ev->u.millis % 1000) * 1000;
+		if (ev->when.tv_usec > 1000000) {
+			ev->when.tv_sec++;
+			ev->when.tv_usec -= 1000000;
+		}
 		break;
 	case PEVENT_MESG_PORT:
 		va_start(args, type);
@@ -392,15 +405,16 @@ pevent_register(struct pevent_ctx *ctx, struct pevent 
 	} else
 		pevent_ctx_notify(ctx);
 
+	/* Caller gets the one reference */
+	ev->peventp = peventp;
+	*peventp = ev;
+
 	/* Add event to the pending event list */
 	PEVENT_ENQUEUE(ctx, ev);
 
 	/* Unlock context */
 	MUTEX_UNLOCK(&ctx->mutex, ctx->mutex_count);
 
-	/* Done; caller gets the one reference */
-	ev->peventp = peventp;
-	*peventp = ev;
 	return (0);
 }
 
@@ -565,6 +579,13 @@ loop:
 	if (ctx->notified) {
 		DBG(PEVENT, "ctx %p thread was notified", ctx);
 		(void)pd_read(ctx->pipe[0], &pevent_byte, 1);
+		ctx->notified = 0;
+	}
+
+	/* If we were intentionally woken up, read the wakeup byte */
+	if (ctx->notified) {
+		DBG(PEVENT, "ctx %p thread was notified", ctx);
+		(void)read(ctx->pipe[0], &pevent_byte, 1);
 		ctx->notified = 0;
 	}