mirror of
https://gitee.com/acl-dev/acl.git
synced 2024-11-30 02:47:56 +08:00
some samples can't run on MacOS
This commit is contained in:
parent
8d96beb9b0
commit
639501690a
@ -1,4 +1,5 @@
|
||||
#include "lib_acl.h"
|
||||
#include "../stamp.h"
|
||||
|
||||
static ACL_ATOMIC *__counter;
|
||||
static unsigned __nproducers = 5;
|
||||
@ -15,7 +16,7 @@ static void* consumer(void *ctx)
|
||||
n = acl_atomic_int64_add_fetch(__counter, 1);
|
||||
|
||||
if (n <= 10) {
|
||||
printf("thread: %ld, n=%lld\r\n", acl_pthread_self(), n);
|
||||
printf("thread: %ld, n=%lld\r\n", (long) acl_pthread_self(), n);
|
||||
}
|
||||
|
||||
o = (void*) acl_mbox_read(box, 0, &succ);
|
||||
@ -51,6 +52,8 @@ int main(int argc, char *argv[])
|
||||
acl_pthread_t *producers, consumer_tid;
|
||||
acl_pthread_attr_t attr;
|
||||
static long long value = 0, counter;
|
||||
struct timeval begin, end;
|
||||
double spent, speed;
|
||||
ACL_MBOX *box;
|
||||
int ch, ret;
|
||||
|
||||
@ -81,6 +84,8 @@ int main(int argc, char *argv[])
|
||||
|
||||
acl_pthread_attr_init(&attr);
|
||||
|
||||
gettimeofday(&begin, NULL);
|
||||
|
||||
ret = acl_pthread_create(&consumer_tid, &attr, consumer, box);
|
||||
|
||||
for (i = 0; i < __nproducers; i++) {
|
||||
@ -97,7 +102,11 @@ int main(int argc, char *argv[])
|
||||
usleep(100);
|
||||
}
|
||||
|
||||
printf("at last, counter=%lld, loop=%lld\r\n", value, counter);
|
||||
gettimeofday(&end, NULL);
|
||||
spent = stamp_sub(&end, &begin);
|
||||
speed = (counter * 1000) / (spent >= 1.0 ? spent : 1.0);
|
||||
printf("counter=%lld, loop=%lld, spent=%.2f ms, speed=%.2f\r\n",
|
||||
value, counter, spent, speed);
|
||||
|
||||
acl_atomic_free(__counter);
|
||||
acl_pthread_join(consumer_tid, NULL);
|
||||
|
20
lib_acl/samples/benchmark/stamp.h
Normal file
20
lib_acl/samples/benchmark/stamp.h
Normal file
@ -0,0 +1,20 @@
|
||||
#ifndef __STAMP_INCLUDE_H__
|
||||
#define __STAMP_INCLUDE_H__
|
||||
|
||||
static double stamp_sub(const struct timeval *from, const struct timeval *sub)
|
||||
{
|
||||
struct timeval res;
|
||||
|
||||
memcpy(&res, from, sizeof(struct timeval));
|
||||
|
||||
res.tv_usec -= sub->tv_usec;
|
||||
if (res.tv_usec < 0) {
|
||||
--res.tv_sec;
|
||||
res.tv_usec += 1000000;
|
||||
}
|
||||
res.tv_sec -= sub->tv_sec;
|
||||
|
||||
return (res.tv_sec * 1000.0 + res.tv_usec/1000.0);
|
||||
}
|
||||
|
||||
#endif
|
@ -1,4 +1,5 @@
|
||||
base_path = ../../..
|
||||
include ../../Makefile.in
|
||||
#CFLAGS += -DLOCK_FREE
|
||||
CFLAGS += -Wno-deprecated-declarations
|
||||
PROG = taskq
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include "lib_acl.h"
|
||||
#include "../stamp.h"
|
||||
#include "taskq.h"
|
||||
|
||||
static ACL_ATOMIC *__counter;
|
||||
@ -9,7 +10,7 @@ static void consumer(void *ctx acl_unused)
|
||||
//int *o = (int*) ctx;
|
||||
long long n = acl_atomic_int64_add_fetch(__counter, 1);
|
||||
if (n <= 10) {
|
||||
printf("thread: %ld, n=%lld\r\n", acl_pthread_self(), n);
|
||||
printf("thread: %ld, n=%lld\r\n", (long)acl_pthread_self(), n);
|
||||
}
|
||||
//free(o);
|
||||
}
|
||||
@ -44,6 +45,8 @@ int main(int argc, char *argv[])
|
||||
TASKQ* taskq;
|
||||
acl_pthread_t *producers;
|
||||
acl_pthread_attr_t attr;
|
||||
struct timeval begin, end;
|
||||
double spent, speed;
|
||||
static long long value = 0, counter;
|
||||
int ch;
|
||||
|
||||
@ -79,6 +82,8 @@ int main(int argc, char *argv[])
|
||||
|
||||
acl_pthread_attr_init(&attr);
|
||||
|
||||
gettimeofday(&begin, NULL);
|
||||
|
||||
for (i = 0; i < nproducers; i++) {
|
||||
int ret = acl_pthread_create(&producers[i], &attr, producer, taskq);
|
||||
assert(ret == 0);
|
||||
@ -93,7 +98,11 @@ int main(int argc, char *argv[])
|
||||
usleep(100);
|
||||
}
|
||||
|
||||
printf("at last, counter=%lld, loop=%lld\r\n", value, counter);
|
||||
gettimeofday(&end, NULL);
|
||||
spent = stamp_sub(&end, &begin);
|
||||
speed = (counter * 1000) / (spent >= 1.0 ? spent : 1);
|
||||
printf("counter=%lld, loop=%lld, spent %.2f ms ,speed=%.2f\r\n",
|
||||
value, counter, spent, speed);
|
||||
|
||||
acl_atomic_free(__counter);
|
||||
taskq_destroy(taskq);
|
||||
|
@ -20,8 +20,12 @@ typedef struct TASKQ {
|
||||
unsigned slot_full;
|
||||
unsigned nthreads;
|
||||
acl_pthread_t *threads;
|
||||
sem_t sem_empty;
|
||||
sem_t sem_full;
|
||||
#if defined(__APPLE__)
|
||||
char *path_empty;
|
||||
char *path_full;
|
||||
#endif
|
||||
sem_t *sem_empty;
|
||||
sem_t *sem_full;
|
||||
} TASKQ;
|
||||
|
||||
static void *taskq_pop(void *ctx);
|
||||
@ -30,6 +34,9 @@ TASKQ *taskq_create(unsigned qsize, unsigned nthreads)
|
||||
{
|
||||
TASKQ *taskq = (TASKQ*) acl_mycalloc(1, sizeof(TASKQ));
|
||||
acl_pthread_attr_t attr;
|
||||
#if defined(__APPLE__)
|
||||
const char *path = ".";
|
||||
#endif
|
||||
int ret, i;
|
||||
|
||||
ret = acl_pthread_mutex_init(&taskq->lock, NULL);
|
||||
@ -43,10 +50,25 @@ TASKQ *taskq_create(unsigned qsize, unsigned nthreads)
|
||||
taskq->nthreads = nthreads;
|
||||
taskq->threads = (acl_pthread_t*) acl_mycalloc(nthreads, sizeof(acl_pthread_t));
|
||||
|
||||
ret = sem_init(&taskq->sem_empty, 0, qsize);
|
||||
#if defined(__APPLE__)
|
||||
assert(path && *path);
|
||||
taskq->path_empty = acl_concatenate(path, "/", "sem_empty", NULL);
|
||||
taskq->sem_empty = sem_open(taskq->path_empty, O_CREAT, S_IRUSR | S_IWUSR, qsize);
|
||||
assert(taskq->sem_empty != SEM_FAILED);
|
||||
#else
|
||||
taskq->sem_empty = (sem_t*) acl_mycalloc(1, sizeof(sem_t));
|
||||
ret = sem_init(taskq->sem_empty, 0, qsize);
|
||||
assert(ret == 0);
|
||||
ret = sem_init(&taskq->sem_full, 0, 0);
|
||||
#endif
|
||||
#if defined(__APPLE__)
|
||||
taskq->path_full = acl_concatenate(path, "/", "sem_full", NULL);
|
||||
taskq->sem_full = sem_open(taskq->path_full, O_CREAT, S_IRUSR | S_IWUSR, 0);
|
||||
assert(taskq->sem_full != SEM_FAILED);
|
||||
#else
|
||||
taskq->sem_full = (sem_t*) acl_mycalloc(1, sizeof(sem_t));
|
||||
ret = sem_init(taskq->sem_full, 0, 0);
|
||||
assert(ret == 0);
|
||||
#endif
|
||||
|
||||
taskq->slot_empty = 0;
|
||||
taskq->slot_full = 0;
|
||||
@ -58,6 +80,7 @@ TASKQ *taskq_create(unsigned qsize, unsigned nthreads)
|
||||
ret = pthread_create(&taskq->threads[i], &attr, taskq_pop, taskq);
|
||||
assert(ret == 0);
|
||||
}
|
||||
printf("ok\n");
|
||||
return taskq;
|
||||
}
|
||||
|
||||
@ -70,8 +93,17 @@ void taskq_destroy(TASKQ *taskq)
|
||||
(void) acl_pthread_join(taskq->threads[i], NULL);
|
||||
}
|
||||
|
||||
(void) sem_destroy(&taskq->sem_empty);
|
||||
(void) sem_destroy(&taskq->sem_full);
|
||||
#if defined(__APPLE__)
|
||||
sem_close(taskq->sem_empty);
|
||||
sem_close(taskq->sem_full);
|
||||
acl_myfree(taskq->path_empty);
|
||||
acl_myfree(taskq->path_full);
|
||||
#else
|
||||
(void) sem_destroy(taskq->sem_empty);
|
||||
(void) sem_destroy(taskq->sem_full);
|
||||
acl_myfree(taskq->sem_empty);
|
||||
acl_myfree(taskq->sem_full);
|
||||
#endif
|
||||
(void) acl_pthread_mutex_destroy(&taskq->lock);
|
||||
acl_myfree(taskq->threads);
|
||||
acl_myfree(taskq->tasks);
|
||||
@ -84,7 +116,7 @@ static void *taskq_pop(void *ctx)
|
||||
TASK task;
|
||||
|
||||
while (1) {
|
||||
int ret = sem_wait(&taskq->sem_full);
|
||||
int ret = sem_wait(taskq->sem_full);
|
||||
if (ret != 0) {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
@ -103,7 +135,7 @@ static void *taskq_pop(void *ctx)
|
||||
ret = acl_pthread_mutex_unlock(&taskq->lock);
|
||||
assert(ret == 0);
|
||||
|
||||
ret = sem_post(&taskq->sem_empty);
|
||||
ret = sem_post(taskq->sem_empty);
|
||||
assert(ret == 0);
|
||||
|
||||
task.callback(task.ctx);
|
||||
@ -114,7 +146,7 @@ static void *taskq_pop(void *ctx)
|
||||
|
||||
void taskq_push(TASKQ *taskq, void (*callback)(void*), void *ctx)
|
||||
{
|
||||
int ret = sem_wait(&taskq->sem_empty);
|
||||
int ret = sem_wait(taskq->sem_empty);
|
||||
assert(ret == 0);
|
||||
|
||||
ret = acl_pthread_mutex_lock(&taskq->lock);
|
||||
@ -127,6 +159,6 @@ void taskq_push(TASKQ *taskq, void (*callback)(void*), void *ctx)
|
||||
ret = acl_pthread_mutex_unlock(&taskq->lock);
|
||||
assert(ret == 0);
|
||||
|
||||
ret = sem_post(&taskq->sem_full);
|
||||
ret = sem_post(taskq->sem_full);
|
||||
assert(ret == 0);
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include "lib_acl.h"
|
||||
#include "../stamp.h"
|
||||
|
||||
static ACL_ATOMIC *__counter;
|
||||
static int __max = 10000;
|
||||
@ -7,7 +8,7 @@ static void consumer(void *ctx acl_unused)
|
||||
{
|
||||
long long n = acl_atomic_int64_add_fetch(__counter, 1);
|
||||
if (n <= 10) {
|
||||
printf("thread: %ld, n=%lld\r\n", acl_pthread_self(), n);
|
||||
printf("thread: %ld, n=%lld\r\n", (long) acl_pthread_self(), n);
|
||||
}
|
||||
}
|
||||
|
||||
@ -38,6 +39,8 @@ int main(int argc, char *argv[])
|
||||
acl_pthread_pool_t *tpool;
|
||||
acl_pthread_attr_t attr;
|
||||
acl_pthread_t *producers;
|
||||
struct timeval begin, end;
|
||||
double spent, speed;
|
||||
static long long value = 0, counter;
|
||||
int ch, debug = 0;
|
||||
|
||||
@ -81,6 +84,8 @@ int main(int argc, char *argv[])
|
||||
|
||||
acl_pthread_attr_init(&attr);
|
||||
|
||||
gettimeofday(&begin, NULL);
|
||||
|
||||
for (i = 0; i < nproducers; i++) {
|
||||
int ret = acl_pthread_create(&producers[i], &attr, producer, tpool);
|
||||
assert(ret == 0);
|
||||
@ -95,7 +100,12 @@ int main(int argc, char *argv[])
|
||||
usleep(100);
|
||||
}
|
||||
|
||||
printf("at last, counter=%lld, loop=%lld\r\n", value, counter);
|
||||
gettimeofday(&end, NULL);
|
||||
spent = stamp_sub(&end, &begin);
|
||||
speed = (counter * 1000) / (spent >= 1.0 ? spent : 1.0);
|
||||
|
||||
printf("counter=%lld, loop=%lld, spent=%.2f ms, speed=%.2f\r\n",
|
||||
value, counter, spent, speed);
|
||||
|
||||
acl_atomic_free(__counter);
|
||||
acl_pthread_pool_destroy(tpool);
|
||||
|
Loading…
Reference in New Issue
Block a user