add sem_create(), wrappign sem_init() for linux and sem_open() for macOS

This commit is contained in:
Ryo Nakamura
2023-03-15 23:54:57 +09:00
parent 3902fb584a
commit ae4b848ba0
3 changed files with 70 additions and 33 deletions

View File

@@ -204,24 +204,9 @@ static int validate_and_set_defaut_params(struct mscp_opts *o)
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_opts *o, struct mscp_ssh_opts *s)
{
char sem_name[PSEMNAMLEN] = "mscp-";
struct mscp *m;
int n;
@@ -253,12 +238,8 @@ struct mscp *mscp_init(const char *remote_host, int direction,
INIT_LIST_HEAD(&m->path_list);
chunk_pool_init(&m->cp);
n = strlen(sem_name);
if (random_string(sem_name + n, sizeof(sem_name) - n - 1) < 0)
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());
if ((m->sem = sem_create(o->max_startups)) == NULL) {
mscp_set_error("sem_create: %s", strerrno());
goto free_out;
}
@@ -708,7 +689,8 @@ void mscp_free(struct mscp *m)
free(m->remote);
if (m->cores)
free(m->cores);
sem_close(m->sem);
sem_release(m->sem);
free(m);
}

View File

@@ -1,19 +1,20 @@
#ifdef __APPLE__
#include <stdlib.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#elif linux
#define _GNU_SOURCE
#include <sched.h>
#include <stdlib.h>
#else
#error unsupported platform
#endif
#include <stdlib.h>
#include <util.h>
#include <platform.h>
#include <message.h>
#ifdef __APPLE__
int nr_cpus()
{
@@ -34,9 +35,38 @@ int set_thread_affinity(pthread_t tid, int core)
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
@@ -64,9 +94,27 @@ int set_thread_affinity(pthread_t tid, int core)
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

View File

@@ -2,13 +2,20 @@
#define _PLATFORM_H_
#include <pthread.h>
#ifndef PSEMNAMLEN /* defined in macOS, but not in Linux */
#define PSEMNAMLEN 31
#endif
#include <semaphore.h>
int nr_cpus(void);
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_ */