mirror of
https://github.com/upa/mscp.git
synced 2026-05-30 11:50:46 +08:00
add sem_create(), wrappign sem_init() for linux and sem_open() for macOS
This commit is contained in:
26
src/mscp.c
26
src/mscp.c
@@ -204,24 +204,9 @@ static int validate_and_set_defaut_params(struct mscp_opts *o)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int random_string(char *buf, size_t size)
|
|
||||||
{
|
|
||||||
char chars[] = "abcdefhijklmnopkwxyz1234567890";
|
|
||||||
int n, x;
|
|
||||||
|
|
||||||
for (n = 0; n < size - 1; n++) {
|
|
||||||
x = get_random(sizeof(chars) - 1);
|
|
||||||
buf[n] = chars[x];
|
|
||||||
}
|
|
||||||
buf[size - 1] = '\0';
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct mscp *mscp_init(const char *remote_host, int direction,
|
struct mscp *mscp_init(const char *remote_host, int direction,
|
||||||
struct mscp_opts *o, struct mscp_ssh_opts *s)
|
struct mscp_opts *o, struct mscp_ssh_opts *s)
|
||||||
{
|
{
|
||||||
char sem_name[PSEMNAMLEN] = "mscp-";
|
|
||||||
struct mscp *m;
|
struct mscp *m;
|
||||||
int n;
|
int n;
|
||||||
|
|
||||||
@@ -253,12 +238,8 @@ struct mscp *mscp_init(const char *remote_host, int direction,
|
|||||||
INIT_LIST_HEAD(&m->path_list);
|
INIT_LIST_HEAD(&m->path_list);
|
||||||
chunk_pool_init(&m->cp);
|
chunk_pool_init(&m->cp);
|
||||||
|
|
||||||
n = strlen(sem_name);
|
if ((m->sem = sem_create(o->max_startups)) == NULL) {
|
||||||
if (random_string(sem_name + n, sizeof(sem_name) - n - 1) < 0)
|
mscp_set_error("sem_create: %s", strerrno());
|
||||||
goto free_out;
|
|
||||||
|
|
||||||
if ((m->sem = sem_open(sem_name, O_CREAT, 600, o->max_startups)) == SEM_FAILED) {
|
|
||||||
mscp_set_error("sem_open: %s", strerrno());
|
|
||||||
goto free_out;
|
goto free_out;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -708,7 +689,8 @@ void mscp_free(struct mscp *m)
|
|||||||
free(m->remote);
|
free(m->remote);
|
||||||
if (m->cores)
|
if (m->cores)
|
||||||
free(m->cores);
|
free(m->cores);
|
||||||
sem_close(m->sem);
|
|
||||||
|
sem_release(m->sem);
|
||||||
free(m);
|
free(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,19 +1,20 @@
|
|||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
|
#include <stdlib.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/sysctl.h>
|
#include <sys/sysctl.h>
|
||||||
#elif linux
|
#elif linux
|
||||||
#define _GNU_SOURCE
|
#define _GNU_SOURCE
|
||||||
#include <sched.h>
|
#include <sched.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#else
|
#else
|
||||||
#error unsupported platform
|
#error unsupported platform
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#include <util.h>
|
#include <util.h>
|
||||||
#include <platform.h>
|
#include <platform.h>
|
||||||
#include <message.h>
|
#include <message.h>
|
||||||
|
|
||||||
|
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
int nr_cpus()
|
int nr_cpus()
|
||||||
{
|
{
|
||||||
@@ -34,9 +35,38 @@ int set_thread_affinity(pthread_t tid, int core)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_random(int max)
|
|
||||||
|
static void random_string(char *buf, size_t size)
|
||||||
{
|
{
|
||||||
return arc4random() % max;
|
char chars[] = "abcdefhijklmnopkwxyz1234567890";
|
||||||
|
int n, x;
|
||||||
|
|
||||||
|
for (n = 0; n < size - 1; n++) {
|
||||||
|
x = arc4random(sizeof(chars) - 1);
|
||||||
|
buf[n] = chars[x];
|
||||||
|
}
|
||||||
|
buf[size - 1] = '\0';
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
sem_t *sem_create(int value)
|
||||||
|
{
|
||||||
|
char sem_name[PSEMNAMLEN] = "mscp-";
|
||||||
|
sem_t *sem;
|
||||||
|
int n;
|
||||||
|
|
||||||
|
n = strlen(sem_name);
|
||||||
|
random_string(sem_name + n, sizeof(sem_name) - n - 1);
|
||||||
|
if ((sem = sem_open(sem_name, O_CREAT, 600, value)) == SEM_FAILED)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
return sem;
|
||||||
|
}
|
||||||
|
|
||||||
|
int sem_release(sem_t *sem)
|
||||||
|
{
|
||||||
|
return sem_close(sem);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -64,9 +94,27 @@ int set_thread_affinity(pthread_t tid, int core)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_random(int max)
|
sem_t *sem_create(int value)
|
||||||
{
|
{
|
||||||
return random() % max;
|
sem_t *sem;
|
||||||
|
|
||||||
|
if ((sem = malloc(sizeof(*sem))) == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (sem_init(sem, 0, value) < 0) {
|
||||||
|
free(sem);
|
||||||
|
return sem;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int sem_release(sem_t *sem)
|
||||||
|
{
|
||||||
|
int ret = sem_close(sem);
|
||||||
|
free(sem);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -2,13 +2,20 @@
|
|||||||
#define _PLATFORM_H_
|
#define _PLATFORM_H_
|
||||||
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
#include <semaphore.h>
|
||||||
#ifndef PSEMNAMLEN /* defined in macOS, but not in Linux */
|
|
||||||
#define PSEMNAMLEN 31
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int nr_cpus(void);
|
int nr_cpus(void);
|
||||||
int set_thread_affinity(pthread_t tid, int core);
|
int set_thread_affinity(pthread_t tid, int core);
|
||||||
int get_random(int max);
|
|
||||||
|
/*
|
||||||
|
* macOS does not support sem_init(). macOS (seems to) releases the
|
||||||
|
* named semaphore when associated mscp process finished. In linux,
|
||||||
|
* program (seems to) need to release named semaphore in /dev/shm by
|
||||||
|
* sem_unlink() explicitly. So, using sem_init() (unnamed semaphore)
|
||||||
|
* in linux and using sem_open() (named semaphore) in macOS without
|
||||||
|
* sem_unlink() are reasonable (?).
|
||||||
|
*/
|
||||||
|
sem_t *sem_create(int value);
|
||||||
|
int sem_release(sem_t *sem);
|
||||||
|
|
||||||
#endif /* _PLATFORM_H_ */
|
#endif /* _PLATFORM_H_ */
|
||||||
|
|||||||
Reference in New Issue
Block a user