forked from liuliu/ccv
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
encounter the problem with libdispatch
have to roll out my own version of dispatch_main_queue :-(
- Loading branch information
Showing
11 changed files
with
187 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include <stdlib.h> | ||
#include <ev.h> | ||
#include <dispatch/dispatch.h> | ||
|
||
typedef struct { | ||
void *context; | ||
void (*cb)(void*); | ||
} main_async_t; | ||
|
||
static dispatch_semaphore_t async_queue_semaphore; | ||
static int queue_position = 0; | ||
static int queue_pending = 0; | ||
static int queue_length = 10; | ||
static main_async_t* async_queue; | ||
static ev_async main_async; | ||
|
||
void dispatch_main_async_f(void* context, void (*cb)(void*)) | ||
{ | ||
dispatch_semaphore_wait(async_queue_semaphore, DISPATCH_TIME_FOREVER); | ||
++queue_pending; | ||
if (queue_pending > queue_length) | ||
{ | ||
queue_length = (queue_length * 3 + 1) / 2; | ||
async_queue = (main_async_t*)realloc(async_queue, sizeof(main_async_t) * queue_length); | ||
} | ||
async_queue[queue_position].context = context; | ||
async_queue[queue_position].cb = cb; | ||
queue_position = (queue_position + 1) % queue_length; | ||
dispatch_semaphore_signal(async_queue_semaphore); | ||
ev_async_send(EV_DEFAULT_ &main_async); | ||
} | ||
|
||
static void main_async_dispatch(EV_P_ ev_async* w, int revents) | ||
{ | ||
dispatch_semaphore_wait(async_queue_semaphore, DISPATCH_TIME_FOREVER); | ||
while (queue_pending > 0) | ||
{ | ||
queue_position = (queue_position + queue_length - 1) % queue_length; | ||
--queue_pending; | ||
async_queue[queue_position].cb(async_queue[queue_position].context); | ||
} | ||
dispatch_semaphore_signal(async_queue_semaphore); | ||
} | ||
|
||
void main_async_init(void) | ||
{ | ||
async_queue_semaphore = dispatch_semaphore_create(1); | ||
async_queue = (main_async_t*)malloc(sizeof(main_async_t) * queue_length); | ||
ev_async_init(&main_async, main_async_dispatch); | ||
} | ||
|
||
void main_async_start(EV_P) | ||
{ | ||
ev_async_start(EV_A_ &main_async); | ||
} | ||
|
||
void main_async_destroy(void) | ||
{ | ||
dispatch_release(async_queue_semaphore); | ||
free(async_queue); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#ifndef _GUARD_async_h_ | ||
#define _GUARD_async_h_ | ||
|
||
void dispatch_main_async_f(void* context, void (*cb)(void*)); | ||
void main_async_init(void); | ||
void main_async_start(EV_P); | ||
void main_async_destroy(void); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include "uri.h" | ||
|
||
#define MSG ("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 12\r\n\r\nhello world\n") | ||
|
||
ebb_buf ebb_bbf_detect_objects(const void* query) | ||
{ | ||
ebb_buf buf; | ||
buf.data = MSG; | ||
buf.len = sizeof(MSG); | ||
return buf; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include "uri.h" | ||
#include <string.h> | ||
|
||
static const ccv_uri_dispatch_t uri_map[] = { | ||
{ | ||
.uri = "/bbf/detect.objects", | ||
.dispatch = ebb_bbf_detect_objects, | ||
}, | ||
}; | ||
|
||
|
||
ccv_uri_dispatch_t* find_uri_dispatch(const char* path) | ||
{ | ||
ccv_uri_dispatch_t* low = (ccv_uri_dispatch_t*)uri_map; | ||
ccv_uri_dispatch_t* high = (ccv_uri_dispatch_t*)uri_map + sizeof(uri_map) / sizeof(ccv_uri_dispatch_t) - 1; | ||
while (low <= high) | ||
{ | ||
ccv_uri_dispatch_t* middle = low + (high - low) / 2; | ||
int flag = strcmp(middle->uri, path); | ||
if (flag == 0) | ||
return middle; | ||
else if (flag < 0) | ||
low = middle + 1; | ||
else | ||
high = middle - 1; | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#ifndef _GUARD_uri_h_ | ||
#define _GUARD_uri_h_ | ||
|
||
#include "ebb.h" | ||
|
||
typedef struct { | ||
char* uri; | ||
ebb_buf (*dispatch)(const void*); | ||
} ccv_uri_dispatch_t; | ||
|
||
ccv_uri_dispatch_t* find_uri_dispatch(const char* path); | ||
ebb_buf ebb_bbf_detect_objects(const void* query); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters