[rtl] Linux equivalents to pthread_make_periodic_np and pthread_suspend_np

2001-04-13 Thread Ivan Martinez



Hello all:
I'm trying to make DSLib v2 generate 
adecuatedsystem calls depending on it beingLinux, RTLinux, or RTAI. 
I'm looking in the Linux threads functions and I don't see any equivalent to 
pthread_make_periodic_np and pthread_suspend_np. Is there any?. If not, how can 
I performsuch operations?.
Thank you.
Ivan Martinez


Re: [rtl] Linux equivalents to pthread_make_periodic_np and pthread_suspend_np

2001-04-13 Thread David Schleef

On Fri, Apr 13, 2001 at 09:22:14PM +0200, Ivan Martinez wrote:
 Hello all:
 I'm trying to make DSLib v2 generate adecuated system calls depending on it being 
Linux, RTLinux, or RTAI. I'm looking in the Linux threads functions and I don't see 
any equivalent to pthread_make_periodic_np and pthread_suspend_np. Is there any?. If 
not, how can I perform such operations?.
 Thank you.
 Ivan Martinez


It can be duplicated using something similar to the attached
file.



dave...




/* Example of a periodic task using straight POSIX calls. */
/* Copyright 2001 David Schleef [EMAIL PROTECTED] */
/* Use, distribution, and modification are allowed under the LGPL. */

#include stdio.h
#include stdlib.h
#include sys/time.h
#include sys/types.h
#include unistd.h


#define PERIOD 10	/* 100 ms */

void tv_add_usec(struct timeval *tv,unsigned int usec);
int tv_compare(struct timeval *a,struct timeval *b);
void tv_sub(struct timeval *a,struct timeval *b);

int main(int argc,char *argv[])
{
	struct timeval now;
	struct timeval next_tick;

	gettimeofday(next_tick,NULL);
	tv_add_usec(next_tick,PERIOD);
	while(1){
		printf("tick\n");

		gettimeofday(now,NULL);
		if(tv_compare(next_tick,now)0){
			struct timeval timeout;

			timeout = next_tick;
			tv_sub(timeout,now);
			select(0,NULL,NULL,NULL,timeout);
		}
		tv_add_usec(next_tick,PERIOD);
	}
}

void tv_add_usec(struct timeval *tv,unsigned int usec)
{
	tv-tv_usec += usec;
	while(tv-tv_usec=100){
		tv-tv_usec -= 100;
		tv-tv_sec ++;
	}
}

int tv_compare(struct timeval *a,struct timeval *b)
{
	if(a-tv_sec==b-tv_sec)return b-tv_usec-a-tv_usec;
	return b-tv_sec-a-tv_sec;
}

void tv_sub(struct timeval *a,struct timeval *b)
{
	a-tv_sec -= b-tv_sec;
	if(a-tv_usec  b-tv_usec){
		a-tv_usec += 100;
		a-tv_sec --;
	}
	a-tv_usec -= b-tv_usec;
}





Re: [rtl] Linux equivalents to pthread_make_periodic_np and pthread_suspend_np

2001-04-13 Thread Bernhard Kuhn

David Schleef schrieb:

 It can be duplicated using something similar to the attached
 file.

select() is not bad, but you can't guarantee a stable
period (means a delay is propagated to the following
periods).

For this reason, i prefer pthread_cond_timedwait()
(see file attached).
Suspending can be done by using pthread_cond_wait().

Bernhard

// gcc -Wall -O2 -I/usr/src/rtai/include -D__KERNEL__ -DMODULE -c rt_thread.c

#include linux/module.h
#include linux/kernel.h
#include linux/version.h
#include asm/io.h

#include rtai.h
#include rtai_sched.h
#include rtai_pthread.h

#define PORT 0x378

pthread_t thread_data;

pthread_mutex_t wait_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t wait_cond = PTHREAD_COND_INITIALIZER;

volatile int kill=0;

int position=0;
MODULE_PARM(position,"i");


void add_ns(struct timespec *time,long ns) {
  time-tv_nsec+=ns;
  time-tv_sec  +=  time-tv_nsec/(10LL);
  time-tv_nsec  =  time-tv_nsec%(10LL);
};


void thread_code(int arg) {

  struct timespec time;
  clock_gettime(CLOCK_REALTIME,time);

  while(!kill) {

outb(0xff,PORT);
add_ns(time,100+position);
pthread_mutex_lock(wait_mutex);
pthread_cond_timedwait(wait_cond,wait_mutex,time);
pthread_mutex_unlock(wait_mutex);

outb(0x00,PORT);
add_ns(time,1900-position);
pthread_mutex_lock(wait_mutex);
pthread_cond_timedwait(wait_cond,wait_mutex,time);
pthread_mutex_unlock(wait_mutex);

  };

  kill=2;
  pthread_exit(0);

};


int init_module(void) {
  printk("rt_task: started, position=%i\n",position);
  rt_set_oneshot_mode();
  start_rt_timer(0);
  pthread_create(thread_data,0,(void*)thread_code,0);
  return 0; 
};


void cleanup_module(void) {
  kill=1;
  while(kill!=2);

  pthread_cond_destroy(wait_cond);
  pthread_mutex_destroy(wait_mutex);

  stop_rt_timer();
  printk("rt_task: stopped, position=%i\n",position);
};



Re: [rtl] Linux equivalents to pthread_make_periodic_np and pthread_suspend_np

2001-04-13 Thread yodaiken

I've come to like pthread_cond_wait, although I admit that I found it
disgusting until recently -- when I wrote some applications where it
was quite pleasant. 

On Sat, Apr 14, 2001 at 02:43:03AM +0200, Bernhard Kuhn wrote:
 David Schleef schrieb:
 
  It can be duplicated using something similar to the attached
  file.
 
 select() is not bad, but you can't guarantee a stable
 period (means a delay is propagated to the following
 periods).
 
 For this reason, i prefer pthread_cond_timedwait()
 (see file attached).
 Suspending can be done by using pthread_cond_wait().
 
 Bernhard
 // gcc -Wall -O2 -I/usr/src/rtai/include -D__KERNEL__ -DMODULE -c rt_thread.c
 
 #include linux/module.h
 #include linux/kernel.h
 #include linux/version.h
 #include asm/io.h
 
 #include rtai.h
 #include rtai_sched.h
 #include rtai_pthread.h
 
 #define PORT 0x378
 
 pthread_t thread_data;
 
 pthread_mutex_t wait_mutex = PTHREAD_MUTEX_INITIALIZER;
 pthread_cond_t wait_cond = PTHREAD_COND_INITIALIZER;
 
 volatile int kill=0;
 
 int position=0;
 MODULE_PARM(position,"i");
 
 
 void add_ns(struct timespec *time,long ns) {
   time-tv_nsec+=ns;
   time-tv_sec  +=  time-tv_nsec/(10LL);
   time-tv_nsec  =  time-tv_nsec%(10LL);
 };
 
 
 void thread_code(int arg) {
 
   struct timespec time;
   clock_gettime(CLOCK_REALTIME,time);
 
   while(!kill) {
 
 outb(0xff,PORT);
 add_ns(time,100+position);
 pthread_mutex_lock(wait_mutex);
 pthread_cond_timedwait(wait_cond,wait_mutex,time);
 pthread_mutex_unlock(wait_mutex);
 
 outb(0x00,PORT);
 add_ns(time,1900-position);
 pthread_mutex_lock(wait_mutex);
 pthread_cond_timedwait(wait_cond,wait_mutex,time);
 pthread_mutex_unlock(wait_mutex);
 
   };
 
   kill=2;
   pthread_exit(0);
 
 };
 
 
 int init_module(void) {
   printk("rt_task: started, position=%i\n",position);
   rt_set_oneshot_mode();
   start_rt_timer(0);
   pthread_create(thread_data,0,(void*)thread_code,0);
   return 0; 
 };
 
 
 void cleanup_module(void) {
   kill=1;
   while(kill!=2);
 
   pthread_cond_destroy(wait_cond);
   pthread_mutex_destroy(wait_mutex);
 
   stop_rt_timer();
   printk("rt_task: stopped, position=%i\n",position);
 };


-- 
-
Victor Yodaiken 
Finite State Machine Labs: The RTLinux Company.
 www.fsmlabs.com  www.rtlinux.com

-- [rtl] ---
To unsubscribe:
echo "unsubscribe rtl" | mail [EMAIL PROTECTED] OR
echo "unsubscribe rtl Your_email" | mail [EMAIL PROTECTED]
--
For more information on Real-Time Linux see:
http://www.rtlinux.org/rtlinux/