summaryrefslogtreecommitdiff
path: root/led/posix_mutex_server/test.c
blob: ce6617af4e1c411ffdcbe1a3fa08bba559c57dea (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
/*
 *  Simple test program -- simplified version of sample test hello.
 */

#include <bsp.h>

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>

#include "../led.h"

pthread_mutex_t Mutex;

void *Server(
  void * ignored
)
{
  int status;

  LED_INIT();

  sleep( 1 );

  for ( ; ; ) {
    status = pthread_mutex_lock( &Mutex );
    if ( status )
      fprintf( stderr, "Server - lock did not work (%d)\n", status );

    LED_OFF();

    status = sleep( 1 );

    status = pthread_mutex_unlock( &Mutex );
    if ( status )
      fprintf( stderr, "Server - unlock did not work (%d)\n", status );


  }
  return NULL;
}

void *POSIX_Init(
  void *argument
)
{
#if defined(DEFINE_MUTEX_ATTRIBUTES)
  pthread_mutexattr_t mutex_attr;
#endif
  pthread_t thread_id;
  int status;

  puts( "\n\n*** LED BLINKER -- pthread_mutex ***" );

#if defined(DEFINE_MUTEX_ATTRIBUTES)
  status = pthread_mutexattr_init( &mutex_attr );
  status = pthread_mutexattr_setprotocol( &mutex_attr, PTHREAD_PRIO_INHERIT );
  status = pthread_mutexattr_setprioceiling( &mutex_attr, 128 );

  status = pthread_mutex_init( &Mutex, &mutex_attr );
#else
  status = pthread_mutex_init( &Mutex, NULL );
#endif
  if (status) 
    fprintf( stderr, "mutex init (%d) \n", status );
  /* fprintf( stderr, "Mutex id --> 0x%08x\n", Mutex ); */

  status = pthread_create( &thread_id, NULL, Server, NULL );
  if (status) 
    fprintf( stderr, "pthread_create --> %d\n", status );

  status = pthread_mutex_lock( &Mutex );
  if (status) 
    fprintf( stderr, "Main -- first lock did not work (%d)\n", status );

  if ( status )
    fprintf( stderr, "Main - lock did not work (%d)\n", status );

  for ( ; ; ) {
    LED_ON();

    status = sleep( 1 );

    status = pthread_mutex_unlock( &Mutex );
    if ( status )
      fprintf( stderr, "Main - unlock did not work (%d)\n", status );

    status = pthread_mutex_lock( &Mutex );
    if ( status )
      fprintf( stderr, "Main - lock did not work (%d)\n", status );



  }
  exit( 0 );
}

/* configuration information */

#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER

#define CONFIGURE_POSIX_INIT_THREAD_TABLE

#define CONFIGURE_MAXIMUM_POSIX_THREADS 2
#define CONFIGURE_MAXIMUM_POSIX_MUTEXES 1

#define CONFIGURE_INIT

#include <rtems/confdefs.h>

/* end of file */