630 likes | 1.02k Views
Concurrency Design Patterns. By: Kalpana Nallavolu Nestor Rivera Feras Batarseh. Design Patterns. A design pattern is a formal way of documenting successful solutions to problems. Reusability, modularity, win time. Resign Pattern Ex: fatal, misbehavior. Contributors.
E N D
Concurrency Design Patterns By: Kalpana Nallavolu Nestor Rivera Feras Batarseh
Design Patterns • A design pattern is a formal way of documenting successful solutions to problems. • Reusability, modularity, win time. • Resign Pattern • Ex: fatal, misbehavior.
Contributors • Christopher Alexander • GoF
Concurrency Design Patterns • Concurrency Pattern • Message Queuing Pattern • Interrupt Pattern • Guarded Call Pattern • Rendezvous Pattern • Cyclic Executive Pattern • Round Robin Pattern • Static Priority Pattern • Dynamic Priority Pattern
Background • Thread • Concurrency • Concurrency Architecture • Active Object • Passive objects
Collaboration Of Objects 2 • The active object accept messages and delegate them to the internally contained application objects for processing. • The issues around the concurrency management are varied depending 1) The threads are completely independent 2) The threads are not really independent.
Message Queuing Pattern • It provides a simples means of communication among threads. • It uses asynchronous communication. • Information shared among threads is passed by value to the separate thread. • Advantages and disadvantages
Interrupt Pattern • In many real-time applications, certain events must be responded quickly and efficiently regardless of what system is doing. • This method is rarely used as only concurrency strategy • Advantages and disadvantages.
Guarded Call Pattern – Abstract • Problem with Message Queuing Pattern…? Asynchronous & slow • What if urgency? Alternative -> synchronously invoke a method of an object • Data integrity is key -> mutual exclusion • Need to avoid ->synchronization & deadlock problems
Guarded Call Pattern – Collaboration Roles I • Server Thread: <<active object>> • Server Object • Shared Resource • Protection with Mutex • Client Thread: <<active object>> • Client object • Synchronously invoke method
Guarded Call Pattern – Collaboration Roles II • Boundary Object • Protected interface to server objects with operations • Mutex • Mutual exclusion semaphore object • Permits only single caller through time • Safer if invoked by relevant operations • Locks and unlocks shared resource • Blocks client threads
Guarded Call Pattern – Collaboration Roles III • Server • Uses the shared resource object • Provides the service to client across the thread boundary • Shared Resource • Provides data or service • Shared by multiple servers • Integrity must be protected => serialization of access
Guarded Call Pattern - Consequences • Timely response (unless services are locked) • Simplification: no interaction among servers => boundary object, contain shared resource & one mutex/server
Guarded Call Pattern – Related Patterns • Message Queuing, Interrupt, Guarded Call & Rendezvous could be mixed • Race conditions may appear • If server is stateful, monitor on server may be needed
Rendezvous Pattern • Simplified form of Guarded Call Pattern (synchronize threads) • Rendezvous Object => means for synchronization (synchronization point, policy or precondition)
Rendezvous Pattern - Abstract • Precondition => specified to be true prior to an action/activity • Behavioral model => each thread registers with Rendezvous class and blocks until released
Rendezvous Pattern – Collaboration Roles I • Callback: holds address of client thread • Client Thread • At synchronization points, register • Pass their callbacks • i.e. notify() called once condition met
Rendezvous Pattern – Collaboration Roles II • Rendezvous • Manages thread synchronization • Has register() operation • Sync Policy • Reifies set of preconditions into single concept i.e. Registration count (Thread Barrier Pattern)
Cyclic Executive Pattern • Very small systems (execution predictability is crucial) • Easy to implement • Can run in memory constraint systems (no RTOS) • Executes tasks in turn, from first to last and starts over again (cycle)
Cyclic Executive Pattern – Collaboration Roles • Abstract Thread • Super class for concrete thread • Interface to the scheduler • Concrete Thread • <<active object>> • Contains passive objects • Scheduler • Initializes system (loads tasks) • Runs each of them in turn in perpetuity
Cyclic Executive Pattern – Properties of Applications • Constant number of tasks • Amount of task time is unimportant or consistent • Tasks are independent • Usage of shared resources complete after each task • Sequential ordering of tasks is adequate
Cyclic Executive Pattern – Pros and Cons • Primary advantage => simplicity • Primary Disadvantages • Lack of flexibility • Non-optimality • Instability to violation of its assumptions • Response to incoming event: deadline >= cycle time • No criticality or urgency: all threads are equally important.
Introduction • Hardcore real time deadline! • A “fairness” scheduling method. • All tasks progress than specific deadline to be met. • Entire set movement.
The Model • Abstract thread: super class, associates with scheduler. • Concrete thread: (active thread) real work of the system. • Scheduler: initialize the tasks and run them. • Stack: control and data; variables, return variables • Task control block • Timer: ticks for scheduler, front end to the hardware timer, switch task();
Pros and Cons • All tasks get the chance to run. • Misbehaving task wont ruin the system. • Scale up better to big systems. • Higher switching time… • All tasks take the same time slice! Priority?
Related Patterns • More complex than cyclic executive • Less complex than priority patterns, discussed next. • Special kind that gives each task its time.
Introduction • Priorities predefined. • Common approach, simple, large number of tasks. • Urgency (time) + Criticality (importance)
The Model • Abstract thread • Blocked queue: queue for TCB, blocked tasks in, out to ready queue. • Concrete thread • Shared resources • Mutex: semaphore object that allows one caller object (thread) to access shared resources. Mutex ID, entry point.