rthreads

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

rthreads

Tim Wiess
Ted,
here's a copy of the powerpc rfork_thread and tas routines that i had
originally put together for another project.

tim

[demime 1.01d removed an attachment of type multipart/mixed]

Reply | Threaded
Open this post in threaded view
|

rfork_therad.S for alpha

Takashi-4
Hello, folks

I start playing rtherads on alpha, so I write rfork_thread.S.
if this is useful for somebody, I am happy.

now, basic functionality seems to work ok with my alpha.

--
KUDO Takashi

=== src/lib/librthread/arch/alpha/rfork_thread.S ===
/* $OpenBSD$ */

/*
 * Copyright (c) 2005 KUDO Takashi <[hidden email]>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <machine/asm.h>
#include "../../../libc/arch/alpha/SYS.h"

/*
 *                        a0           a1            a2                  a3
 * int rfork_thread(int flags, void *stack, void (*func)(void *), void *arg);
 */
        .globl rfork_thread
        .ent rfork_thread
rfork_thread:
        LDGP(pv)
#define FSZ 32 /* frame size */
        lda sp, -FSZ(sp) /* space for saving ra, a3 and v0 */
        stq ra, 0(sp)
        stq a3, 8(sp) /* rfork uses a3 */
        .frame sp, FSZ, ra
        .prologue 1

        jsr ra, rfork
        LDGP(ra)
        beq v0, 1f

        /* parent process */
        ldq ra, 0(sp)
        lda sp, FSZ(sp)
        RET
1:
        /* child process */
        ldq a0, 8(sp)
        mov a1, sp /* setup stack */
        mov a2, pv
        jsr ra, (pv), 0 /* funcall func(arg) */
        LDGP(ra)

        jsr ra, threxit
        /* not reached */
        .end rfork_thread
=== src/lib/librthread/arch/alpha/rfork_thread.S ===

=== pth-test.c ===
/* gcc pth-test.c -lrthread */
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

int *sum(int *max)
{
  int i, sum;
  int *r;

  for (i = 1, sum = 0; i <= *max; i++)
    sum += i;

  r = (int *)malloc(sizeof(int));
  *r = sum;

  return r;
}

int main()
{
  pthread_t thr1, thr2;
  int *r1, *r2;
  int max1 = 10, max2 = 10;

  if (pthread_create(&thr1, NULL, (void*(*)(void*))sum, &max1) < 0) {
    perror("pthread_create1");
    exit(1);
  }
  if (pthread_create(&thr2, NULL, (void*(*)(void*))sum, &max2) < 0) {
    perror("pthread_create2");
    exit(2);
  }

  pthread_join(thr1, (void**)&r1);
  pthread_join(thr2, (void**)&r2);

  printf("Sum(1..%d)+Sum(1..%d)=%d\n", max1, max2, *r1 + *r2);

  free(r1);
  free(r2);

  return 0;
}
=== pth-test.c ===