16
Multi-threaded programming with NSPR Larry Hardiman

Multi-threaded programming with NSPR

  • Upload
    lavonn

  • View
    29

  • Download
    0

Embed Size (px)

DESCRIPTION

Multi-threaded programming with NSPR. Larry Hardiman. Overview. What is NSPR? Why program using threads? What you need to program with threads NSPR’s thread abstraction Thread synchronization using locks Thread synch using condition variables Techniques for high performance threads. - PowerPoint PPT Presentation

Citation preview

Page 1: Multi-threaded programming with NSPR

Multi-threaded programming with NSPR

Larry Hardiman

Page 2: Multi-threaded programming with NSPR

Overview

• What is NSPR?

• Why program using threads?

• What you need to program with threads

• NSPR’s thread abstraction

• Thread synchronization using locks

• Thread synch using condition variables

• Techniques for high performance threads

Page 3: Multi-threaded programming with NSPR

What is NSPR?

• Original NSPR 1.0 was NS Java base

• NSPR 2.0 began in early 1996

• NSPR 2 is platform abstraction

• No GUI

• Threading, thread sync

• File and Socket I/O

• Time, IPC, ...

Page 4: Multi-threaded programming with NSPR

Why program using threads?

• Simplify program design

• Improve performance when calling blocking functions

• Utilize all processors on multi-processor systems

Page 5: Multi-threaded programming with NSPR

Thread Programming Basics

• Thread Management

• Thread private data

• Thread synchronization

Page 6: Multi-threaded programming with NSPR

Thread Management

• PR_CreateThread()

• PR_Interrupt()

• PR_JoinThread()

• PR_SetThreadPriority()

• PR_GetThreadPriority()

Page 7: Multi-threaded programming with NSPR

Creating a thread

1 #include “prthread.h”

2 PRThread *3 PR_CreateThread(4 PRThreadType type, /* user or system */5 (void)(start)(void *arg), /* function */6 void *arg; /*argument to start function */7 PRThreadPriority priority, /* low, norm … */8 PRThreadScope scope, /* local, global */9 PRThreadState state, /* joinable? */10 PRInt32 stacksize );

Page 8: Multi-threaded programming with NSPR

Thread Private Data

Think of “thread private data” as global variables, but for exclusive use for a single thread.

Void (*PR_ThreadPrivateDTOR)(void*);PRStatus PR_NewThreadPrivateIndex( int *ndx, (dtor));PRStatus PR_SetThreadPrivate( ndx, *priv );void* PR_GetThreadPrivate( ndx );

See: mozilla/nsprpub/pr/tests/tpd.c

Page 9: Multi-threaded programming with NSPR

Thread sync using PRLock

1 #include “prlock.h”

2 PRLock *lock;

3 lock = PR_NewLock();4 PR_Lock(lock);5 /* operate on your data … */6 PR_Unlock(lock);7 PR_DestroyLock(lock);

Page 10: Multi-threaded programming with NSPR

Using Condition Variables

1 PRIntn data = 0; /* Thread A */2 PRLock *lock = PR_NewLock();3 PRCondVar *cvar = PR_NewCondVar(lock);

4 PR_Lock(lock);5 while( data == 0 )6 PR_WaitCondVar( cvar, timeout ); /* lock released while blocked. */ /* re-locked at fall through */7 data = 0; /* satisfy Thread B’s condition */8 PR_NotifyCondVar( cvar );9 PR_Unlock( lock );

Page 11: Multi-threaded programming with NSPR

Using Condition Variables

/* Thread B */

4 PR_Lock(lock);5 while( data == 1 )6 PR_WaitCondVar( cvar, timeout ); /* lock released while blocked. */ /* re-locked at fall through */7 data = 1;8 PR_NotifyCondVar( cvar );9 PR_Unlock( lock );

Page 12: Multi-threaded programming with NSPR

What Not to Do

1 PRLock *lock;2 PRCondVar *cv;

3 PR_Lock( lock );4 PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT ); /* implies state in *cv */5 /* do something */6 PR_Unlock( lock );

There is no state in a PRCondVar.

Page 13: Multi-threaded programming with NSPR

… More What Not to Do

1 PRBool dataIsAvailable = PR_FALSE;

2 PR_Lock( lock );3 if ( !dataIsAvailable )4 PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT );

/* Bad! dataIsAvailable may change during wait */5 /* work on the available data */6 PR_Unlock( lock );

The lock is released during PR_WaitCondvar(). Other threads can change dataIsAvailable. Use the while() loop;test, wait, test again.

Page 14: Multi-threaded programming with NSPR

Condvar Example

A hacked cvar.c goes here.

Page 15: Multi-threaded programming with NSPR

High Performance Threads

• … you are protecting data, not code

• partition assets so that you protect the smallest piece possible

• hold exclusive control as short a time as possible

Page 16: Multi-threaded programming with NSPR

Bibliography

• David R. Butenhof, "Programming with POSIX Threads", Addison-Wesley, 1997

• Steve Kleiman, et.al, “Programming with Threads”, Prentice Hall, 1996

• NSPR group, “NSPR Reference”, Ch. 1, 3, 5, 6. http://www.mozilla.org/projects/nspr/reference/html/index.html

• UseNet newsgroup: comp.programming.threads.