Added thread api and pthread impl
authorDerek Atkins <derek@ihtfp.com>
Mon, 27 Jan 2003 22:46:17 +0000 (22:46 +0000)
committerDerek Atkins <derek@ihtfp.com>
Mon, 27 Jan 2003 22:46:17 +0000 (22:46 +0000)
removed 64-bit dependency on rpc

shib-target/Makefile.am
shib-target/shib-threads.cpp [new file with mode: 0644]
shib-target/shib-threads.h [new file with mode: 0644]
shib-target/shibrpc-xdr.c
shib-target/shibrpc.h
shib-target/shibrpc.x

index 2b553c0..73b48d1 100644 (file)
@@ -11,7 +11,7 @@ AM_CXXFLAGS = -I${top_srcdir}/oncrpc
 endif
 
 libshib_targetdir = $(includedir)/shib-target
-libshib_target_HEADERS = shib-target.h shibrpc.h
+libshib_target_HEADERS = shib-target.h shib-threads.h shibrpc.h
 
 libshib_target_la_SOURCES = \
        shib-ccache.cpp \
@@ -26,6 +26,7 @@ libshib_target_la_SOURCES = \
        shib-shire.cpp \
        shib-sock.c \
        shib-target.cpp \
+       shib-threads.cpp \
        shibrpc-clnt.c \
        shibrpc-server.cpp \
        shibrpc-svc.c \
diff --git a/shib-target/shib-threads.cpp b/shib-target/shib-threads.cpp
new file mode 100644 (file)
index 0000000..ee63e65
--- /dev/null
@@ -0,0 +1,140 @@
+/*
+ * shib-threads.cpp -- an abstraction around Pthreads
+ *
+ * Created by: Derek Atkins <derek@ihtfp.com>
+ *
+ * $Id$
+ */
+
+#include <shib-target/shib-threads.h>
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#ifdef HAVE_PTHREAD
+#include <pthread.h>
+#else
+#error "You need to create the proper thread implementation"
+#endif
+
+#include <stdexcept>
+
+using namespace std;
+using namespace shibtarget;
+
+
+// pthread implementation of the Shib Target Threads API
+
+//
+// "Private" Implementation
+//
+
+class ThreadImpl : public Thread {
+public:
+  ThreadImpl(void* (*start_routine)(void*), void* arg);
+  ~ThreadImpl() {}
+
+  int detach() { return pthread_detach(thread_id); }
+  int join(void** thread_return) { return pthread_join(thread_id, thread_return); }
+
+  pthread_t    thread_id;
+};
+
+class MutexImpl : public Mutex {
+public:
+  MutexImpl();
+  ~MutexImpl() { pthread_mutex_destroy (&mutex); }
+
+  int lock() { return pthread_mutex_lock (&mutex); }
+  int unlock() { return pthread_mutex_unlock (&mutex); }
+
+  pthread_mutex_t mutex;
+};
+
+class CondWaitImpl : public CondWait {
+public:
+  CondWaitImpl();
+  ~CondWaitImpl() { pthread_cond_destroy (&cond); }
+
+  int wait(Mutex* mutex) { return wait (dynamic_cast<MutexImpl*>(mutex)); }
+  int wait(MutexImpl* mutex) { return pthread_cond_wait (&cond, &(mutex->mutex)); }
+  int timedwait(Mutex* mutex, struct timespec *abstime)
+       { return timedwait (dynamic_cast<MutexImpl*>(mutex), abstime); }
+  int timedwait(MutexImpl* mutex, struct timespec *abstime) 
+       { return pthread_cond_timedwait (&cond, &(mutex->mutex), abstime); }
+  int signal() { return pthread_cond_signal (&cond); }
+  int broadcast() { return pthread_cond_broadcast (&cond); }
+
+  pthread_cond_t cond;
+};
+
+class RWLockImpl : public RWLock {
+public:
+  RWLockImpl();
+  ~RWLockImpl() { pthread_rwlock_destroy (&lock); }
+
+  int rdlock() { return pthread_rwlock_rdlock (&lock); }
+  int wrlock() { return pthread_rwlock_wrlock (&lock); }
+  int unlock() { return pthread_rwlock_unlock (&lock); }
+
+  pthread_rwlock_t lock;
+};
+
+
+
+// Constructor Implementation follows...
+
+ThreadImpl::ThreadImpl(void* (*start_routine)(void*), void* arg)
+{
+  if (pthread_create (&thread_id, NULL, start_routine, arg) != 0)
+    throw runtime_error("pthread_create failed");
+}
+
+MutexImpl::MutexImpl()
+{
+  if (pthread_mutex_init (&mutex, NULL) != 0)
+    throw runtime_error("pthread_mutex_init failed");
+}
+
+CondWaitImpl::CondWaitImpl()
+{
+  if (pthread_cond_init (&cond, NULL) != 0)
+    throw runtime_error("pthread_cond_init failed");
+}
+
+RWLockImpl::RWLockImpl()
+{
+  if (pthread_rwlock_init (&lock, NULL) != 0)
+    throw runtime_error("pthread_rwlock_init failed");
+}
+
+
+//
+// public "static" creation functions
+//
+
+Thread* Thread::create(void* (*start_routine)(void*), void* arg)
+{
+  return new ThreadImpl(start_routine, arg);
+}
+
+void Thread::exit(void* return_val)
+{
+  pthread_exit (return_val);
+}
+    
+Mutex * Mutex::create()
+{
+  return new MutexImpl();
+}
+
+CondWait * CondWait::create()
+{
+  return new CondWaitImpl();
+}
+
+RWLock * RWLock::create()
+{
+  return new RWLockImpl();
+}
diff --git a/shib-target/shib-threads.h b/shib-target/shib-threads.h
new file mode 100644 (file)
index 0000000..fa72714
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * shib-threads.h -- abstraction around Pthreads interface
+ *
+ * Created by: Derek Atkins <derek@ihtfp.com>
+ *
+ * $Id$
+ */
+
+#ifndef SHIB_THREADS_H
+#define SHIB_THREADS_H
+
+#ifdef __cplusplus
+
+#include <time.h>
+
+namespace shibtarget {
+
+  class Thread {
+  public:
+    static Thread* create(void* (*start_routine)(void*), void* arg);
+    static void exit(void* return_val);
+
+    virtual int detach() = 0;
+    virtual int join(void** thread_return) = 0;
+  };
+
+  class Mutex {
+  public:
+    static Mutex* create();
+
+    virtual int lock() = 0;
+    virtual int unlock() = 0;
+  };
+
+  class Lock {
+  public:
+    Lock(Mutex* mtx) { mutex = mtx; mutex->lock(); }
+    ~Lock() { mutex->unlock(); }
+
+  private:
+    Lock(const Lock&);
+    void operator=(const Lock&);
+    Mutex* mutex;
+  };
+
+  class CondWait {
+  public:
+    static CondWait* create();
+
+    virtual int wait(Mutex*) = 0;
+    virtual int timedwait(Mutex*, struct timespec *abstime) = 0;
+    virtual int signal() = 0;
+    virtual int broadcast() = 0;
+  };
+
+  class RWLock {
+  public:
+    static RWLock* create();
+
+    virtual int rdlock() = 0;
+    virtual int wrlock() = 0;
+    virtual int unlock() = 0;
+  };
+
+} // namespace
+
+#endif /* __cplusplus */
+#endif /* SHIB_THREADS_H */
index fa3c3ff..2402d07 100644 (file)
@@ -52,9 +52,9 @@ xdr_shibrpc_session_is_valid_args_1 (XDR *xdrs, shibrpc_session_is_valid_args_1
                 return FALSE;
         if (!xdr_bool (xdrs, &objp->checkIPAddress))
                 return FALSE;
-        if (!xdr_uint64_t (xdrs, &objp->lifetime))
+        if (!xdr_long (xdrs, &objp->lifetime))
                 return FALSE;
-        if (!xdr_uint64_t (xdrs, &objp->timeout))
+        if (!xdr_long (xdrs, &objp->timeout))
                 return FALSE;
        return TRUE;
 }
index 1045e71..225868c 100644 (file)
@@ -15,12 +15,6 @@ extern "C" {
 #endif
 
 
-/* Define NEED_XDR_LONGLONG in the cases where uint64_t is not defined */
-#ifdef NEED_XDR_LONGLONG
-#define xdr_uint64_t xdr_ulonglong_t
-#endif
-
-
 enum ShibRpcStatus {
        SHIBRPC_OK = 0,
        SHIBRPC_UNKNOWN_ERROR = 1,
@@ -53,8 +47,8 @@ typedef struct ShibRpcAssertion_1 ShibRpcAssertion_1;
 struct shibrpc_session_is_valid_args_1 {
        ShibRpcHttpCookie_1 cookie;
        bool_t checkIPAddress;
-       uint64_t lifetime;
-       uint64_t timeout;
+       long lifetime;
+       long timeout;
 };
 typedef struct shibrpc_session_is_valid_args_1 shibrpc_session_is_valid_args_1;
 
index b40fe9d..4a57008 100644 (file)
 %
 #endif
 
-#ifdef RPC_HDR
-%
-%/* Define NEED_XDR_LONGLONG in the cases where uint64_t is not defined */
-%#ifdef NEED_XDR_LONGLONG
-%#define xdr_uint64_t xdr_ulonglong_t
-%#endif
-%
-#endif
-
 enum ShibRpcStatus {
   SHIBRPC_OK = 0,
   SHIBRPC_UNKNOWN_ERROR = 1,
@@ -63,8 +54,8 @@ struct ShibRpcAssertion_1 {
 struct shibrpc_session_is_valid_args_1 {
   ShibRpcHttpCookie_1  cookie;
   bool                 checkIPAddress;
-  uint64_t             lifetime;
-  uint64_t             timeout;
+  long                 lifetime;
+  long                 timeout;
 };
 
 struct shibrpc_session_is_valid_ret_1 {