Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <ctype.h>
- #include <ncurses.h>
- #include <sys/time.h>
- #define NMAX 100
- #define STATESX 4 //number of states
- #define EVENTSX 3 //number of distinct events
- #define STATE1 0x01 //event 0
- #define STATE2 0x02 //event 1
- #define TYPE_S 0x01 //event 0
- #define TYPE_N 0x02 //event 1
- #define TYPE_OTHER 0x04 //event 2
- pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
- volatile int count = 0; //global variables used by threads must be volatile
- volatile char events2[NMAX];
- volatile unsigned int events,state,state_events;
- volatile clock_t cook_time, current_tick,next_tick;
- volatile int keep_warm, key,fcount,scount ;
- volatile unsigned int tps, target_count = 0;
- volatile int l = 0;
- volatile int h = 0;
- volatile int m = 0;
- volatile int s = 0;
- void null()
- {
- // printf("null");
- }
- void increase()
- {
- l++;
- printf("zwiekszono l");
- }
- void increaseH()
- {
- h++;
- if(h>=24)
- h = 0;
- printf("zwiekszono h, %d", h);
- }
- void increaseM()
- {
- m++;
- if(m>=60){
- m = 0;
- increaseH();
- }
- printf("zwiekszono m, %d", m);
- }
- void increaseS()
- {
- s++;
- if(s>=60){
- s = 0;
- increaseM();
- }
- printf("zwiekszono s, %d", s);
- }
- void output()
- {
- printf("h:m:s = %d:%d:%d", h,m,s);
- }
- typedef void (*TPT)();
- struct state
- {
- unsigned int active;
- unsigned int next_state;
- TPT action;
- };
- struct state state_table[EVENTSX][STATESX] = {
- //INPUTH //OUTPUT //INPUTM //INPUTS //Events
- { {1,2,null}, {1,0,null}, {1,3,null}, {1,1,null}}, //0 TYPE_S
- { {1,0,increaseH}, {1,1,output}, {1,2,increaseM}, {1,3,increaseS}}, //1 TYPE_N
- { {1,0,null}, {1,1,output}, {1,2,null}, {1,3,null}} //2 TYPE_OTHER
- };
- unsigned int getmask(unsigned int staten)
- {
- unsigned int mask = 0;
- int i;
- for(i = EVENTSX-1;i>=0;i--)
- {
- mask = mask << 1;
- mask |= state_table[i][staten].active;
- }
- return mask;
- }
- /*
- geteventn, using the current atate's event mask to select
- only relevant events it then identifies the active event
- with the highest priority, returning its number
- */
- unsigned int getevenyn()
- {
- unsigned int i,bitmask = 1;
- state_events = events & getmask(state);
- for(i = 0;i<EVENTSX;i++)
- {
- if (state_events & bitmask) //mask out irrelevant
- { //events
- events &= ~bitmask; //cancel selected event flag
- return i; //returns event with the highest priority
- }
- bitmask = bitmask << 1;
- }
- return ~0;
- }
- void sort(void) //sort events - brute force solution
- {
- char dum;
- for(int i=0;i<count-1;i++)
- for(int j=i+1;j<count;j++)
- {
- if (events2[j]>events2[i])
- {
- dum = events2[j];
- events2[j]=events2[i];
- events2[i]=dum;
- }
- }
- }
- void* function1( void* arg )
- {
- char ch,ch1;
- while( 1 ) {
- s++;
- if(s>=60) {
- s = 0;
- m++;
- if(m>=60) {
- m = 0;
- h++;
- if(h>=24) {
- h=0;
- }
- }
- }
- sleep(1);
- }
- return 0;
- }
- void* function2( void* arg )
- {
- while( 1 ) {
- fprintf( stderr,"Count is %d\n", count );
- for(int i=0;i<count;i++)
- {
- fprintf(stderr,"%d %c, ",i,events2[i]);
- }
- fprintf(stderr,"\n");
- if (count>0)
- {
- pthread_mutex_lock( &mutex );
- for(int i=0;i<count-1;i++)
- events2[i] = events2[i+1]; //remove data from the head of events
- count--;
- pthread_mutex_unlock( &mutex );
- }
- sleep(5);
- }
- return 0;
- }
- int main( void )
- {
- pthread_create( NULL, NULL, &function1, NULL );
- // pthread_create( NULL, NULL, &function2, NULL );
- unsigned int last_state, event, last_event = 0;
- scount = 0;
- fcount = 0;
- state = 1;
- while(1)
- {
- scount++;
- key = 0;
- if ((key=getchar()) != -1 && key != '\n') //slow laundry - set event flags from slowly arriving data
- {
- if (key == 's') events |= TYPE_S;
- else if (key == 'n') events |= TYPE_N;
- else {
- events |= TYPE_OTHER;
- }
- }
- if ( (event=getevenyn()) != ~0) //periodic dispatcher
- {
- last_event = event;
- last_state = state;
- (*(state_table[event][state].action))(); //transition ACTION
- state = state_table[event][state].next_state; //go to next state
- if (state != last_state) events = 0; //clear event flags if change of states
- }
- //fprintf(stderr,"last_event: %d ,last_state: %d, ",last_event,last_state);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement