-
Notifications
You must be signed in to change notification settings - Fork 0
/
component.h
128 lines (116 loc) · 4.17 KB
/
component.h
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
/*
* Functions for working with components.
*
*
* Copyright (c) 2013, Pavlo Bazilinskyy <pavlo.bazilinskyy@gmail.com>
* School of Computer Science, St. Andrews University
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "ThreadSafeList.h"
#include "GlobalVars.h"
#include "DAL_mem.h"
#include "affinity.h"
#include "Bool.h"
#include "semaphore.h"
#ifndef COMPONENT_H
#define COMPONENT_H
#endif
typedef void (*behaviour_ft)(void *, int, void **);
typedef struct IComponent_data *IComponentPNTR, IComponentStruct;
struct IComponent_data { // The supertype for all components
void (*decRef)(void *pntr); // for ref counting garbage collection
bool stopped; // Has the component been stopped
pthread_t behav_thread; // Process implementing behaviour, runs on a POSIX thread
sem_t component_create_sem; // Semaphore for component_create
};
/*
* Structure used for a wrapper function used in pthread_create.
* Hint from: http://stackoverflow.com/questions/8976419/passing-more-than-one-parameter-to-pthread-create
*/
struct argStructType {
int argc;
void ** argv;
behaviour_ft behaviour;
void * this_ptr;
};
/*
* Wrapper to be used in pthread_create function
*/
void * startRoutine(void * args);
/*
* Will create a new component.
*
*
* THIS_PTR *ptr = compoenet_create();
*
*
* request component referenced by this_ptr to terminate itself the next time it
* tests its loop condition
*
*
* int core - ID of a core for manually setting affinity. -1 by default: affinity should be set manually.
*/
void *component_create(behaviour_ft behaviour, int struct_size, int stack_size, int argc, void *argv[], int core);
/*
* Will casue the termination of a componenet whos 'this' pointer is passsed in.
*
* this_ptr: the returned pointer from the component_create call who you
* wish to terminate.
*
* THIS_PTR *ptr = component_create();
*
* component_stop(ptr);
*
*
* request component referenced by this_ptr to terminate itself the next time it
* tests its loop condition
*/
void component_stop(void *this_ptr);
/*
* currently running component terminates itself
* there is no return; after destroying the calling
* component, the scheduler schedules ready to run component.
* Destruction will also tidy up any channels associated with the component
*/
void component_exit();
/**
* Relinquish control of the processor
*
* This function declares that a component wants to give up control
* of the proccessor.
*
* NOTE you may only need either a blank function here (if you want to avoid modifying the compiler)
* or you can leave this out and change compiler so that it doesn't make calls to yield.
*/
void component_yield(void);
/*
* Initial function that will act as the behaviour for the primordial component.
*
* This function will be the first behaviour run in the system.
*
* It is expected that this function will be used for the component creation
* as well as initial channel bindings and will be defined in the user code
*
*/
void primordial_main(void *this);