Revision 60

View differences:

logic/trunk/src/init/devicelist.h
18 18
device *getDevice(device *dev, char *devName);
19 19
device *getAvailableDevice(device *dev);
20 20
int     getLocalFormattedTime();
21

  
21
int     deleteDeviceList(device *dev);
22 22
/*
23 23
void updateDeviceList(device_d *deviceList); // called on HMI change
24 24
char *getDeviceName();
logic/trunk/src/init/devicelist.c
146 146
    return tm_p->tm_hour*100+tm_p->tm_min;
147 147
}
148 148

  
149
int deleteDeviceList(device *dev)
150
{
151

  
152
    return 1;
153
}
149 154
/*
150 155
void updateDeviceList(device_d *deviceList)
151 156
{
logic/trunk/src/logics/device_threads.h
1 1
#ifndef DEVICE_THREADS_H_INCLUDED
2 2
#define DEVICE_THREADS_H_INCLUDED
3
#include <pthread.h>
4

  
5
typedef struct thread_id_holder_s
6
{   pthread_t                 pthr_idx;
7
    struct thread_id_holder_s *nxt;
8
} thread_id_holder;
3 9

  
4 10
void *randomValThread_routine(void *dummy);
5 11

  
logic/trunk/src/startup/main.c
8 8
#include "../hci_comm/hci_comm.h"
9 9
#include "../init/scenariolist.h"
10 10
#include "../init/devicelist.h"
11
//#include "../logics/device_threads.h"
11
#include "../logics/device_threads.h"
12 12
//#include "../logics/scenario_threads.h"
13 13
#include <stdio.h>
14 14
#include <pthread.h>
......
18 18
// Inter-thread shared variables
19 19
// TODO: These variables could be configuration variables
20 20
//       (eg: static boolean loggingEnabled)
21
static device   *hDevice;   //device list
22
static scenario *hScenario; //scenario list
23

  
21
static device           *hDevice;   //device list
22
static scenario         *hScenario; //scenario list
23
static thread_id_holder *hThrIDhold_dev;//holds IDs of running threads (devices)
24
static thread_id_holder *hThrIDhold_scn;//holds IDs of running threads (scenarios)
24 25
//extern scenario *test;   //device list
25 26

  
26 27
// Crtitical section mutual exclusion security
27 28
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
29
pthread_t       decisionMakingThread;
30
pthread_t       hciConnectionThread;
28 31

  
29 32
// Global variable to check, if HCI made any changes that need
30 33
// to be updated by the logic.
......
48 51
void *hciConnectionThread_routine(void *dummy);
49 52
void *randomValThread_routine(void *dummy);
50 53
void *scenarioThread_routine(void *dummy);
54
int  reinitialiseLogic(); // on HCI request
51 55

  
52 56
// Application entry point
53 57
int main(int argc, char *argv[]) {
......
61 65

  
62 66
    /* regular work */
63 67
    //*
64
	pthread_t hciConnectionThread;
65
	pthread_t decisionMakingThread;
66

  
67
	pthread_t *decisionMakingThread2;
68

  
69 68
	pthread_create(&hciConnectionThread, NULL,hciConnectionThread_routine, NULL);
70 69
	pthread_create(&decisionMakingThread,NULL,decisionMakingThread_routine,NULL);
71 70

  
72 71
	pthread_join(hciConnectionThread,NULL);
73
	pthread_join(decisionMakingThread,NULL);
72
	//pthread_join(decisionMakingThread,NULL);
74 73
    /**/
75 74
	exit(0);
76 75
}
......
81 80
    printf("-- decisionMakingThread_routine started --\n");
82 81

  
83 82
    /**dummy threads for input devices (sensors)**/
84
    // TODO check for possible BUG involving declaring pthread_t as pointer!
85
    pthread_t *deviceThread, *deviceThreadHead;
86
    deviceThreadHead = NULL;
83
    hThrIDhold_dev = NULL;
84
    thread_id_holder *t_holder, *t_holder_old; //just for list of IDs creation, for both scenarios and devices
85
    t_holder_old = NULL;
87 86

  
88 87
    int i;
89 88
    for(i=0; i<getNumberOfDevices(); i++)
90 89
    {
91
        deviceThread = (pthread_t *)malloc(sizeof(pthread_t));
92
        pthread_create(&deviceThread,NULL,randomValThread_routine,NULL);
90
        t_holder = (thread_id_holder *)malloc(sizeof(thread_id_holder));
91
        pthread_create(&t_holder->pthr_idx,NULL,randomValThread_routine,NULL);
93 92

  
94
        if(deviceThreadHead==NULL)
95
            deviceThreadHead=deviceThread;
93
        t_holder->nxt=NULL;
94
        if(t_holder_old!=NULL)
95
            t_holder_old->nxt=t_holder;
96
        t_holder_old=t_holder;
97

  
98
        if(hThrIDhold_dev==NULL)
99
            hThrIDhold_dev=t_holder_old;
96 100
    }
97 101

  
98 102
    /**start scenario threads**/
99
    pthread_t *scenarioThread, *scenarioThreadHead;
100
    scenarioThreadHead = NULL;
103
    t_holder_old = NULL;
104
    hThrIDhold_scn=NULL;
101 105

  
102 106
    for(i=0; i<getNumberOfScenarios(); i++)
103 107
    {
104
        scenarioThread = (pthread_t *)malloc(sizeof(pthread_t));
105
        pthread_create(&scenarioThread,NULL,scenarioThread_routine,NULL);
108
        t_holder = (thread_id_holder *)malloc(sizeof(thread_id_holder));
109
        pthread_create(&t_holder->pthr_idx,NULL,scenarioThread_routine,NULL);
106 110

  
107
        if(scenarioThreadHead==NULL)
108
            scenarioThreadHead=scenarioThread;
109
    }
111
        t_holder->nxt=NULL;
112
        if(t_holder_old!=NULL)
113
            t_holder_old->nxt=t_holder;
114
        t_holder_old=t_holder;
110 115

  
111
    /**wait for threads to finish**/
112
    // TODO bad position of both for loops below, joining of theese threads should be handled in a different way
113
    deviceThread = deviceThreadHead;
114
    for(i=0; i<getNumberOfDevices(); i++)
115
    {
116
        pthread_join(deviceThread,NULL);
117
        deviceThread++;
116
        if(hThrIDhold_scn==NULL)
117
            hThrIDhold_scn=t_holder_old;
118 118
    }
119 119

  
120
    scenarioThread = scenarioThreadHead;
121
    for(i=0; i<getNumberOfScenarios(); i++)
122
    {
123
        pthread_join(scenarioThread,NULL);
124
        scenarioThread++;
125
    }
126

  
127 120
    /*start some sort of control over scenarios? - hmm morda ne...bom jutri :)*/
128

  
121
    printf("Exiting main thread\n");
129 122
	pthread_exit(0);
130 123
}
131 124

  
......
145 138
    device   *inDev;
146 139
    int      active = 0;
147 140
    float    scenarioBuffer;
141
    //printf("My scen ID> %ld\n",pthread_self());
148 142

  
149 143
    /*load scenario*/
150 144
    scen = getAvailableScenario(hScenario);
......
219 213
   	pthread_exit(0);
220 214
}
221 215

  
216
void freeDeviceList(device *dev)
217
{
218
    if(dev->nxt!=NULL)
219
        freeDeviceList(dev->nxt);
220
    free(dev);
221
}
222

  
223
void freeScenarioList(scenario *scn)
224
{
225
    if(scn->nxt!=NULL)
226
        freeScenarioList(scn->nxt);
227
    free(scn);
228
}
229

  
230
void freeThreadIDList(thread_id_holder *tih)
231
{
232
    if(tih->nxt!=NULL)
233
        freeThreadIDList(tih->nxt);
234
    free(tih);
235
}
236

  
237
int reinitialiseLogic()
238
{
239
    thread_id_holder *hold;
240
    hold = hThrIDhold_dev;
241
    //hThrIDhold_dev = NULL;
242

  
243
    //kill device threads
244
    printf("Attempting to cancel threads!\n");
245
    while(hold)
246
    {
247
        printf("Cancelling dev thread %ld with return state %d\n", hold->pthr_idx,pthread_cancel(hold->pthr_idx));
248
        //TODO free thread_id_holder list too!
249
        hold=hold->nxt;
250
    }
251

  
252
    //kill scenario threads
253
    hold = hThrIDhold_scn;
254
    //hThrIDhold_scn = NULL;
255
    while(hold)
256
    {
257
        printf("Cancelling scn thread %ld with return state %d\n", hold->pthr_idx,pthread_cancel(hold->pthr_idx));
258
        //TODO free thread_id_holder list too!
259
        hold=hold->nxt;
260
    }
261

  
262
    //free memory
263
    printf("Freeing memory!\n");
264
    freeDeviceList(hDevice);
265
    freeScenarioList(hScenario);
266
    freeThreadIDList(hThrIDhold_dev);
267
    freeThreadIDList(hThrIDhold_scn);
268

  
269
    //reinit devices
270
    printf("Reinitializing devices!\n");
271
    hDevice   = setDeviceList();
272

  
273
    //reinit scenarios
274
    printf("Reinitializing scenarios!\n");
275
    hScenario = setNewScenarioList(hDevice);
276

  
277
    //restart main thread
278
    printf("Restarting main thread!\n");
279
	pthread_create(&decisionMakingThread,NULL,decisionMakingThread_routine,NULL);
280

  
281
	return 1; //sucess, TODO add fail conditions
282
}

Also available in: Unified diff