enhance(API): Add multi-threaded call
[ric-plt/lib/rmr.git] / src / rmr / common / src / mt_call_static.c
1 // :vi sw=4 ts=4 noet:
2 /*
3 ==================================================================================
4         Copyright (c) 2019 Nokia
5         Copyright (c) 2018-2019 AT&T Intellectual Property.
6
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10
11            http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18 ==================================================================================
19 */
20
21 /*
22         Mnemonic:       mt_call_static.c
23         Abstract:       Static multi-threaded call support.
24
25         Author:         E. Scott Daniels
26         Date:           17  May 2019
27 */
28
29 #ifndef mtcall_static_c
30 #define mtcall_static_c
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <netdb.h>
35 #include <errno.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <unistd.h>
42 #include <netdb.h>
43 #include <time.h>
44
45 #include <semaphore.h>
46
47
48 /*
49         Initialises the chutes that are then hung off of the context.
50
51         Returns > 0 on success; 0 on failure with errno set.
52 */
53 static int init_mtcall( uta_ctx_t*      ctx ) {
54         int                     rc = 1;                 // return code 1== good.
55         int                     i;
56         chute_t*        chutes;
57
58         if( ctx == NULL ) {
59                 errno = EINVAL;
60                 return 0;
61         }
62
63         chutes = ctx->chutes = (chute_t *) malloc( sizeof( chute_t ) * (MAX_CALL_ID+1) );
64         if( chutes == NULL ) {
65                 return 0;
66         }
67
68         for( i = 0; i < MAX_CALL_ID; i++ ) {                            // initialise all of the semaphores
69                 chutes[i].mbuf = NULL;
70                 if( sem_init( &chutes[i].barrier, 0, 0 ) < 0 ) {
71                         fprintf( stderr, "[ERR] rmr: unable to initialise mt call chute [%d]: %s\n", i, strerror( errno ) );
72                         rc = -1;
73                 }
74         }       
75
76
77         return rc;
78 }
79
80 #endif