summaryrefslogtreecommitdiffstats
path: root/code/jasmine/server.h
blob: 23dbed54637caad09a31c3f5f122535c59f81494 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*##############################################################################
# Copyright (c) 2017 ZTE Coreporation and others.
# hu.zhijiang@zte.com.cn
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################*/

#ifndef _MCAST_SERVER_H
#define _MCAST_SERVER_H

#include <pthread.h>
#include <semaphore.h>

struct semaphores {
    sem_t wait_parent;
    sem_t wait_child;
};

struct semlink {
    struct semaphores *this; /* used by parent to point to the struct semaphores
                                which it created during spawn child. */
    struct semaphores *parent; /* used by child to point to the struct
                                  semaphores which it created by parent */
};

void sl_wait_child(struct semlink *sl);
void sl_release_child(struct semlink *sl);
void sl_wait_parent(struct semlink *sl);
void sl_release_parent(struct semlink *sl);

/* Server state machine */
#define S_PREP 0
#define S_SYNC 1
#define S_SEND 2

extern int wait_count;
extern int client_count;
extern int server_port;
extern struct in_addr local_addr;
extern int sfd;

void *udp_server_main(void *args);
void *tcp_server_main(void *args);
void init_semaphores(struct semaphores *sp);
struct semaphores * spawn_thread(pthread_t *pchild,
                                 void *(*entry)(void *),
                                 int create_sems);

#define V(x) do { \
    if (sem_post(&x) < 0) { \
        crit("sem_post()"); \
    } \
} while(0)

#define P(x) do { \
    if (sem_wait(&x) != 0) { \
        crit("sem_wait()"); \
    } \
} while(0)

#endif