/ src / hal / components / threads.c
threads.c
  1  /********************************************************************
  2  * Description:  threads.c
  3  *               This file, 'threads.c', is a HAL component that 
  4  *               provides a way to create realtime threads but 
  5  *               contains no other functionality.
  6  *
  7  * Author: John Kasunich
  8  * License: GPL Version 2
  9  *    
 10  * Copyright (c) 2003 All rights reserved.
 11  *
 12  * Last change: 
 13  ********************************************************************/
 14  /** This file, 'threads.c', is a HAL component that provides a way to 
 15      create realtime threads but contains no other functionality.
 16      It will mostly be used for testing - when EMC is run normally,
 17      the motion module creates all the neccessary threads.
 18      
 19      The module has three pairs of parameters, "name1, period1", etc.
 20  */
 21  
 22  /** Copyright (C) 2003 John Kasunich
 23                         <jmkasunich AT users DOT sourceforge DOT net>
 24  */
 25  
 26  /** This program is free software; you can redistribute it and/or
 27      modify it under the terms of version 2 of the GNU General
 28      Public License as published by the Free Software Foundation.
 29      This library is distributed in the hope that it will be useful,
 30      but WITHOUT ANY WARRANTY; without even the implied warranty of
 31      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 32      GNU General Public License for more details.
 33  
 34      You should have received a copy of the GNU General Public
 35      License along with this library; if not, write to the Free Software
 36      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 37  
 38      THE AUTHORS OF THIS LIBRARY ACCEPT ABSOLUTELY NO LIABILITY FOR
 39      ANY HARM OR LOSS RESULTING FROM ITS USE.  IT IS _EXTREMELY_ UNWISE
 40      TO RELY ON SOFTWARE ALONE FOR SAFETY.  Any machinery capable of
 41      harming persons must have provisions for completely removing power
 42      from all motors, etc, before persons enter any danger area.  All
 43      machinery must be designed to comply with local and national safety
 44      codes, and the authors of this software can not, and do not, take
 45      any responsibility for such compliance.
 46  
 47      This code was written as part of the EMC HAL project.  For more
 48      information, go to www.linuxcnc.org.
 49  */
 50  
 51  #include "rtapi.h"		/* RTAPI realtime OS API */
 52  #include "rtapi_app.h"		/* RTAPI realtime module decls */
 53  #include "hal.h"		/* HAL public API decls */
 54  #include "rtapi_string.h"
 55  
 56  /* module information */
 57  MODULE_AUTHOR("John Kasunich");
 58  MODULE_DESCRIPTION("Thread Module for HAL");
 59  MODULE_LICENSE("GPL");
 60  static char *name1 = "thread1";	/* name of thread */
 61  RTAPI_MP_STRING(name1, "name of thread 1");
 62  static int fp1 = 1;		/* use floating point? default = yes */
 63  RTAPI_MP_INT(fp1, "thread1 uses floating point");
 64  static long period1 = 1000000;	/* thread period - default = 1ms thread */
 65  RTAPI_MP_LONG(period1,  "thread1 period (nsecs)");
 66  static char *name2 = NULL;	/* name of thread */
 67  RTAPI_MP_STRING(name2, "name of thread 2");
 68  static int fp2 = 1;		/* use floating point? default = yes */
 69  RTAPI_MP_INT(fp2, "thread2 uses floating point");
 70  static long period2 = 0;	/* thread period - default = no thread */
 71  RTAPI_MP_LONG(period2, "thread2 period (nsecs)");
 72  static char *name3 = NULL;	/* name of thread */
 73  RTAPI_MP_STRING(name3, "name of thread 3");
 74  static int fp3 = 1;		/* use floating point? default = yes */
 75  RTAPI_MP_INT(fp3, "thread1 uses floating point");
 76  static long period3 = 0;	/* thread period - default = no thread */
 77  RTAPI_MP_LONG(period3, "thread3 period (nsecs)");
 78  
 79  /***********************************************************************
 80  *                STRUCTURES AND GLOBAL VARIABLES                       *
 81  ************************************************************************/
 82  
 83  /* other globals */
 84  static int comp_id;		/* component ID */
 85  
 86  /***********************************************************************
 87  *                  LOCAL FUNCTION DECLARATIONS                         *
 88  ************************************************************************/
 89  
 90  
 91  /***********************************************************************
 92  *                       INIT AND EXIT CODE                             *
 93  ************************************************************************/
 94  
 95  
 96  int rtapi_app_main(void)
 97  {
 98      int retval;
 99  
100      /* have good config info, connect to the HAL */
101      comp_id = hal_init("threads");
102      if (comp_id < 0) {
103  	rtapi_print_msg(RTAPI_MSG_ERR, "THREADS: ERROR: hal_init() failed\n");
104  	return -1;
105      }
106      /* was 'period' specified in the insmod command? */
107      if ((period1 > 0) && (name1 != NULL) && (*name1 != '\0')) {
108  	/* create a thread */
109  	retval = hal_create_thread(name1, period1, fp1);
110  	if (retval < 0) {
111  	    rtapi_print_msg(RTAPI_MSG_ERR,
112  		"THREADS: ERROR: could not create thread '%s'\n", name1);
113  	    hal_exit(comp_id);
114  	    return -1;
115  	} else {
116  	    rtapi_print_msg(RTAPI_MSG_INFO, "THREADS: created %ld uS thread\n", period1 / 1000);
117  	}
118      }
119      if ((period2 > 0) && (name2 != NULL) && (*name2 != '\0')) {
120  	/* create a thread */
121  	retval = hal_create_thread(name2, period2, fp2);
122  	if (retval < 0) {
123  	    rtapi_print_msg(RTAPI_MSG_ERR,
124  		"THREADS: ERROR: could not create thread '%s'\n", name2);
125  	    hal_exit(comp_id);
126  	    return -1;
127  	} else {
128  	    rtapi_print_msg(RTAPI_MSG_INFO, "THREADS: created %ld uS thread\n", period2 / 1000);
129  	}
130      }
131      if ((period3 > 0) && (name3 != NULL) && (*name3 != '\0')) {
132  	/* create a thread */
133  	retval = hal_create_thread(name3, period3, fp3);
134  	if (retval < 0) {
135  	    rtapi_print_msg(RTAPI_MSG_ERR,
136  		"THREADS: ERROR: could not create thread '%s'\n", name3);
137  	    hal_exit(comp_id);
138  	    return -1;
139  	} else {
140  	    rtapi_print_msg(RTAPI_MSG_INFO, "THREADS: created %ld uS thread\n", period3 / 1000);
141  	}
142      }
143      hal_ready(comp_id);
144      return 0;
145  }
146  
147  void rtapi_app_exit(void)
148  {
149      hal_exit(comp_id);
150  }
151