summaryrefslogtreecommitdiffstats
path: root/libccnx-transport-rta/ccnx/transport/transport_rta/core/rta_Framework_Threaded.c
blob: ade2c0a189c34e3507342e6fb5007ba0554adb99 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <config.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

#include <errno.h>

#include <string.h>
#include <fcntl.h>
#include <sys/socket.h>

#define __STDC_FORMAT_MACROS
#include <inttypes.h>

#include <LongBow/runtime.h>

#include <parc/algol/parc_Memory.h>

#include "rta_Framework.h"
#include "rta_ConnectionTable.h"
#include "rta_Framework_Commands.h"

#ifndef DEBUG_OUTPUT
#define DEBUG_OUTPUT 0
#endif

// the thread function
static void *_rtaFramework_Run(void *ctx);

/**
 * Starts the worker thread.  Blocks until started
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
void
rtaFramework_Start(RtaFramework *framework)
{
    pthread_attr_t attr;

    // ensure we're in the INIT state, then bump to STARTING
    // %%% LOCK
    rta_Framework_LockStatus(framework);
    if (framework->status == FRAMEWORK_INIT) {
        framework->status = FRAMEWORK_STARTING;
        rta_Framework_BroadcastStatus(framework);
        rta_Framework_UnlockStatus(framework);
        // %%% UNLOCK
    } else {
        RtaFrameworkStatus status = framework->status;
        rta_Framework_UnlockStatus(framework);
        // %%% UNLOCK
        assertTrue(0, "Invalid state, not FRAMEWORK_INIT, got %d", status);
        return;
    }


    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

    if (pthread_create(&framework->thread, &attr, _rtaFramework_Run, framework) != 0) {
        perror("pthread_create");
        exit(EXIT_FAILURE);
    }

    if (DEBUG_OUTPUT) {
        printf("%s framework started %p\n", __func__, (void *) framework);
    }

    // wait for notificaiton from event thread
    rta_Framework_LockStatus(framework);
    while (framework->status == FRAMEWORK_INIT) {
        rta_Framework_WaitStatus(framework);
    }
    rta_Framework_UnlockStatus(framework);

    if (DEBUG_OUTPUT) {
        printf("%s framework running %p\n", __func__, (void *) framework);
    }
}

static void *
_rtaFramework_Run(void *ctx)
{
    RtaFramework *framework = (RtaFramework *) ctx;

    // %%% LOCK
    rta_Framework_LockStatus(framework);
    if (framework->status != FRAMEWORK_STARTING) {
        assertTrue(0, "Invalid state, expected before %d, got %d", FRAMEWORK_STARTING, framework->status);
        rta_Framework_UnlockStatus(framework);
        // %%% UNLOCK
        pthread_exit(NULL);
    }
    framework->status = FRAMEWORK_RUNNING;

    // Set our thread name, only used to diagnose a crash or in debugging
#if __APPLE__
    pthread_setname_np("RTA Framework");
#else
    pthread_setname_np(framework->thread, "RTA Framework");
#endif

    rta_Framework_BroadcastStatus(framework);
    rta_Framework_UnlockStatus(framework);
    // %%% UNLOCK

    if (DEBUG_OUTPUT) {
        const int bufferLength = 1024;
        char frameworkName[bufferLength];
        pthread_getname_np(framework->thread, frameworkName, bufferLength);
        printf("Framework thread running: '%s'\n", frameworkName);
    }

    // blocks
    parcEventScheduler_Start(framework->base, PARCEventSchedulerDispatchType_Blocking);

    if (DEBUG_OUTPUT) {
        printf("%9" PRIu64 " %s existed parcEventScheduler_Start\n", framework->clock_ticks, __func__);
    }

    // %%% LOCK
    rta_Framework_LockStatus(framework);
    framework->status = FRAMEWORK_SHUTDOWN;
    rta_Framework_BroadcastStatus(framework);
    rta_Framework_UnlockStatus(framework);
    // %%% UNLOCK

    pthread_exit(NULL);
}

/**
 * Stops the worker thread by sending a CommandShutdown.
 * Blocks until shutdown complete.
 *
 * CALLED FROM API's THREAD
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
void
rtaFramework_Shutdown(RtaFramework *framework)
{
    RtaCommand *shutdown = rtaCommand_CreateShutdownFramework();
    rtaCommand_Write(shutdown, framework->commandRingBuffer);
    parcNotifier_Notify(framework->commandNotifier);
    rtaCommand_Release(&shutdown);

    // now block on reading status
    rtaFramework_WaitForStatus(framework, FRAMEWORK_SHUTDOWN);
}