120 likes | 262 Views
NSPR API Overview . Srinivas Lingutla Wan-Teh Chang Lawrence Hardiman. NSPR 2.0. Netscape Portable Runtime Provides OS/system-level services with a platform- independent API. General purpose platform for use by clients and servers Supported on a large number of platforms including
E N D
NSPR API Overview Srinivas Lingutla Wan-Teh Chang Lawrence Hardiman
NSPR 2.0 • Netscape Portable Runtime • Provides OS/system-level services with a platform- independent API. • General purpose platform for use by clients and servers • Supported on a large number of platforms including • - AIX, Digital Unix, HP-UX, Irix, Solaris, Win95, Windows NT, Linux • - Mac, Win 16, SunOS 4.x, NCR, SCO Unix, Netware, etc,. • Used in most products at Netscape
Basic services • Libnspr • Threads and Synchronization • File and Network I/O • Memory Management • Time Management • Library Management • Support for 64-bit platforms • Atomic Operations • Environment Variables • Instrumentation • Process creation • List management • Debug aids
Miscellaneous libraries • Libplc • String functions • Command line options processing • Libplds • Arenas • Hash tables • Events • Libnsps • Reader/Writer locks
Overview • C level API • All services exported through functions (a few are macros) • All exported symbols have the • PR prefix for libnspr • PL prefix for the auxiliary libraries • PS prefix for libnsps • All functions that allocate memory for the caller have a corresponding “free” function • Example: PR_smprintf/PR_smprintf_free • Call PR_GetError to get the NSPR error code when a function fails • Call PR_GetOSError to get the OS error code of the last failed system call in NSPR • All timeout values specified in PRIntervalTime units.
NSPR Threads • Scheduling scope classification • LOCAL • Scheduled by NSPR • Implemented for use by the Client • User-level implementation, faster performance • GLOBAL • Scheduled by the system • Available on all platforms where native threads are present • Each NSPR thread maps to a native thread • User or kernel-level implementation
Thread Models • GLOBAL threads only • - NSPR threads map to pthreads on • AIX 4.2 , Digital Unix 4.0 , HP-UX 11.0 • Solaris 2.5.1, Irix 6.2, Linux 2.1 • - NSPR threads map to Win32 threads on • WIN95 • GLOBAL and LOCAL threads • Irix 6.2 • Global threads map to sprocs • Local threads scheduled by NSPR • WinNT 4.0 • Global threads map to Win32 threads • Local threads are NT Fibers scheduled by NSPR
NSPR Thread APIs • Manipulating threads • PR_CreateThread( PRThreadType type, • void (*start)(void *), • void *arg, • PR_ThreadPriority priority, • PR_ThreadScope scope, • PR_ThreadState, • PRUint32 stackSize) • PR_JoinThread • PR_GetThreadPriority • PR_SetThreadPriority • PR_Interrupt • PR_ClearInterrupt • PR_Sleep
NSPR Thread APIs • Thread Info • PR_GetCurrentThread • PR_GetThreadScope • PR_GetThreadType • PR_GetThreadState • Thread local storage • PR_NewThreadPrivateIndex • PR_SetThreadPrivate • PR_GetThreadPrivate
Locks • PRLock • Classic mutual exclusion locking • Non-reentrant • Not interruptible • Protect data, not code • Use locks for protection, not scheduling • Only hold locks for short periods • Lock management • PRLock *PR_NewLock(void) • PR_DestroyLock(PRLock *) • Lock usage • PR_Lock/PR_Unlock(PRLock *)
Condition variables • PRCondVar • Synchronization between threads - one or more threads can wait for a condition to occur and another thread can notify them when the condition occurs • PRCondVar * PR_NewCondVar(PRLock *) • PR_DestroyCondVar(PRCondVar *) • PR_WaitCondVar(PRCondVar *, PRIntervalTime) • PRLock is unlocked before blocking and reacquired before resuming • PR_NotifyCondVar/PR_NotifyAllCondVar(PRCondVar *) • Notification(s) lost when no thread is waiting
Condition variables - usage • Waiting • Correct method Incorrect • PR_Lock(..) PR_Lock(…) • while (!condition) if (!condition) • PR_WaitCondVar(..) PR_WaitCondVar(..) • …process data …process data • PR_Unlock(…) PR_Unlock(…) • Notification • PR_Lock(..) • ….process data • PR_NotifyCondVar(..) • PR_Unlock(…)