sledge-serverless-framework icon indicating copy to clipboard operation
sledge-serverless-framework copied to clipboard

Is the request scheduler deque variant missing locking?

Open bushidocodes opened this issue 3 years ago • 2 comments

/* TODO: Should this be used???  */
static pthread_mutex_t global_request_scheduler_deque_mutex = PTHREAD_MUTEX_INITIALIZER;

bushidocodes avatar Dec 10 '21 20:12 bushidocodes

What do you mean? Isn't the deque variant supposed to be lock-free?

emil916 avatar Dec 10 '21 20:12 emil916

Relevant comments in deque.h

	/* Use mutual exclusion locks around push/pop if multi-threaded. */                         \
	static inline int deque_push_##name(struct deque_##name *q, type *w)                        \
	{                                                                                           \
		long ct, cb;                                                                        \
                                                                                                    \
		ct = q->top;                                                                        \
		cb = q->bottom;                                                                     \
                                                                                                    \
		/* nope, fixed size only */                                                         \
		if (q->size - 1 < (cb - ct)) return -ENOSPC;                                        \
                                                                                                    \
		q->wrk[cb] = *w;                                                                    \
		__sync_synchronize();                                                               \
		if (__sync_bool_compare_and_swap(&q->bottom, cb, cb + 1) == false) assert(0);       \
                                                                                                    \
		return 0;                                                                           \
	}                                                                                           \
                                                                                                    \
	/* Use mutual exclusion locks around push/pop if multi-threaded. */                         \
	static inline int deque_pop_##name(struct deque_##name *q, type *w)                         \
	{                                                                                           \
		long ct = 0, sz = 0;                                                                \
		long cb  = q->bottom - 1;                                                           \
		int  ret = 0;                                                                       \
                                                                                                    \
		if (__sync_bool_compare_and_swap(&q->bottom, cb + 1, cb) == false) assert(0);       \
                                                                                                    \
		ct = q->top;                                                                        \
		sz = cb - ct;                                                                       \
		if (sz < 0) {                                                                       \
			if (__sync_bool_compare_and_swap(&q->bottom, cb, ct) == false) assert(0);   \
                                                                                                    \
			return -ENOENT;                                                             \
		}                                                                                   \
                                                                                                    \
		*w = q->wrk[cb];                                                                    \
		if (sz > 0) return 0;                                                               \
                                                                                                    \
		ret = __sync_bool_compare_and_swap(&q->top, ct, ct + 1);                            \
		if (__sync_bool_compare_and_swap(&q->bottom, cb, ct + 1) == false) assert(0);       \
		if (ret == false) {                                                                 \
			*w = NULL;                                                                  \
			return -ENOENT;                                                             \
		}                                                                                   \
                                                                                                    \
		return 0;                                                                           \
	}   

bushidocodes avatar Mar 23 '22 16:03 bushidocodes