Threading in COM What is an Apartment and Why Do I Care?

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Maximizing COM+ Application Throughput Steve Swartz Program Manager COM+ Team Microsoft Corporation
How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.
Lecture 10 Sharing Resources. Basics of File Sharing The core component of any server is its ability to share files. In fact, the Server service in all.
Remote Procedure Call (RPC)
CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
Lightweight Remote Procedure Call Brian N. Bershad, Thomas E. Anderson, Edward D. Lazowska, and Henry M. Levy Presented by Alana Sweat.
System Security Scanning and Discovery Chapter 14.
Case Study: Distributed OS Distributed Systems, Lecture # 17.
Hadar Vorenshtein & Meital Levy Instructor yavgeni Rivkin.
Communication in Distributed Systems –Part 2
70-270, MCSE/MCSA Guide to Installing and Managing Microsoft Windows XP Professional and Windows Server 2003 Chapter Nine Managing File System Access.
PRASHANTHI NARAYAN NETTEM.
Lesson 3 Remote Method Invocation (RMI) Mixing RMI and sockets Rethinking out tic-tac-toe game.
RSS RSS is a method that uses XML to distribute web content on one web site, to many other web sites. RSS allows fast browsing for news and updates.
Fundamentals of Python: From First Programs Through Data Structures
1 Chapter Overview Introduction to Windows XP Professional Printing Setting Up Network Printers Connecting to Network Printers Configuring Network Printers.
WebReport/400 TCP/IP Configuration Presented by Kisco Information Systems.
Installing Active Directory on Windows Server 2008 R2 Installing Active Directory on a fresh Windows Server 2008 R2 machine in a home network. These instructions.
Introduction to Data Structures. Data Structures A data structure is a scheme for organizing data in the memory of a computer. Some of the more commonly.
CIS NET Applications1 Chapter 2 –.NET Component- Oriented Programming Essentials.
Nachos Phase 1 Code -Hints and Comments
Threading Models in Visual Basic Language Student Name: Danyu Xu Student ID:98044.
Microsoft’s Distributed Component Object Model (DCOM) Jim Ries Updated 10/5/1999 A semi-technical overview.
Chapter 4 Initial Configuration Tasks. Understanding the Initial Configuration Tasks window Microsoft now provides a new feature, the Initial Configuration.
Installing SIGNZ on a stand- alone machine. These slides will guide you through the installation of the SIGNZ ‘server’ and ‘client’ components on one machine.
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
OOMI From COM to DCOM.
Introduction and Features of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++
Oracle Data Integrator Transformations: Adding More Complexity
June-Hyun, Moon Computer Communications LAB., Kwangwoon University Chapter 26 - Threads.
3.1 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8 th Edition Chapter 3: Processes.
Sound DirectMusic & DirectSound. DirectShow Video Formats DirectShow is an open architecture, it can support any format as long as there are filters to.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
OOMI A short introduction to Microsoft's COM From COM to DCOM.
Processes CSCI 4534 Chapter 4. Introduction Early computer systems allowed one program to be executed at a time –The program had complete control of the.
Remote Procedure Call Andy Wang Operating Systems COP 4610 / CGS 5765.
Mark Stanovich Operating Systems COP Primitives to Build Distributed Applications send and receive Used to synchronize cooperating processes running.
Virtualization Technology and Microsoft Virtual PC 2007 YOU ARE WELCOME By : Osama Tamimi.
Thread basics. A computer process Every time a program is executed a process is created It is managed via a data structure that keeps all things memory.
4343 X2 – Outline The Domain Name System The Web.
Computer Science Lecture 3, page 1 CS677: Distributed OS Last Class: Communication in Distributed Systems Structured or unstructured? Addressing? Blocking/non-blocking?
Intro to Web Services Dr. John P. Abraham UTPA. What are Web Services? Applications execute across multiple computers on a network.  The machine on which.
Linux Operations and Administration Chapter Twelve Configuring a Mail Server.
Integrity Check As You Well Know, It Is A Violation Of Academic Integrity To Fake The Results On Any.
Computer Science Lecture 4, page 1 CS677: Distributed OS Last Class: RPCs RPCs make distributed computations look like local computations Issues: –Parameter.
CopperDroid Logan Horton. Android - Background Android is complicated to analyse due to having 2 places to check for code execution Normally, code is.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
INTRODUCTION TO DATA STRUCTURES 1. DATA STRUCTURES A data structure is a scheme for organizing data in the memory of a computer. Some of the more commonly.
Client-Server Communication
Protecting Memory What is there to protect in memory?
Apartments and COM Threading Models
Protecting Memory What is there to protect in memory?
Microsoft’s Distributed Component Object Model (DCOM)
Out-of-Process Components
CMSC621: Advanced Operating Systems Advanced Operating Systems
Chapter 4: Threads.
Lecture 4: RPC Remote Procedure Call Coulouris et al: Chapter 5
Sarah Diesburg Operating Systems COP 4610
Lecture 4: RPC Remote Procedure Call CDK: Chapter 5
Prof. Leonardo Mostarda University of Camerino
COM, DCOM and Software Components
Out-of-Process Components
Jim Fawcett CSE775 – Distributed Objects Spring 2006
Jim Fawcett CSE775 – Distributed Objects Spring 2004
Lecture 7 ATL and Out-of-Process Components
Jim Fawcett CSE681 – Software Modeling and Analysis Fall 2007
Last Class: Communication in Distributed Systems
How to install and manage exchange server 2010 OP Saklani.
Presentation transcript:

Threading in COM What is an Apartment and Why Do I Care?

What is an Apartment An execution context Contains at least one thread Contains at least one COM object (to be useful) Can contain other restrictions (COM+ contexts) Types of Apartments Multi-Threaded Apartment (MTA) Single-Thread Apartment (STA) MAIN STA Thread Neutral Apartment (TNA)

How does code call methods Thread is executing some other code COM object offers some useful method Thread calls that method Sets up a stack frame Changes IP to the beginning of the method Very simple; very efficient The thread and the object must be in the same apartment.

Threads create apartments Any thread that wants to use COM must call CoInitialize or CoInitializeEx The COINIT parameter to CoInitializeEx determines the apartment type COINIT_APARTMENTTHREADED = STA COINIT_MULTITHREADED = MTA (will join existing) CoInitialize legacy method calls Ex version with COINIT_APARTMENTTHREADED

Apartment model of a process

Process at startup

COM process with apartments

Adding COM objects COM threads create COM objects CoCreateInstance or CoCreateInstanceEx myObject->get_Application() Cardinal rule is, “Create the object in the same apartment as the creator thread.” Apartments are an example of a COM interceptor Checks to see if the object is compatible Creates new threads/apartments as needed

After first CoInitialize call

Sometime later… We have created new threads that called CoInitializeEx(0, COINIT_MULTITHREADED); Some of these COM threads have created COM objects For now, we assume the cardinal rule Next we will look at apartment interception Now our process looks something like this…

Multiple apartment process

Why all the bother? COM servers and client may be written by completely different groups When a COM client (written by Company A) calls a COM object (written by Company B)… Is this COM object compatible with this client? More specifically, is the threading model of the object compatible with the execution environment (apartment) of the thread? Bottom line, is the COM object thread-safe?

Two threads calling…

Incompatible thread & object What if the creating thread is in the MTA but the COM object is not thread-safe Where do we put the new object? Can’t put it in the MTA – other threads may call Can’t put it in an STA – the creating thread can’t call Enter marshalling

Types of marshalling Remote machine Call a method of an object on another computer Uses underlying networking – RPC by default Out of process Call a method of an object that lives in another process on the same machine Uses IPC – LRPC by default Cross-apartment within a process Our problem here Uses Windows message queues by default

Cross-apartment COM calls

Apartment interception Threading models None (Single) – legacy (Main STA) Apartment – Always create in an STA Free – Always create in the MTA Both – Always create in the apartment of the creator thread Neutral – More on this one later Applies only to DLL, in-process COM server Only problem when the thread calling the method and the thread executing the method can be the same thread.

COM threading models

Free-Threaded Marshaler FTM is an aggregated, special IMarshal interface If a COM objects exposes the FTM, it can be called directly by any thread in the same process. Aggregate the FTM by calling CoCreateFreeThreadedMarshaler When the interface is marshaled into the calling apartment, a bogus proxy is created that is a direct pointer to the real interface.

Thread Neutral Apartment TNA is a “special” STA Max one per process; No thread “lives” in the TNA Only one thread at a time can be “in” the apartment. Objects in the apartment are intrinsically thread- safe. A thread “enters” the apartment, makes its calls on the objects there, and then leaves. Other threads must wait until the apartment is empty.

Process with TNA

Threading in.EXE servers No automatic apartment interception Object will be created in the apartment of its creator thread Threads still declare their apartments when calling CoInitializeEx It is your responsibility that an MTA thread does not create a non-thread-safe object It is your responsibility that the objects are created in the most efficient apartment

Additional Resources Essential COM, Don Box Chapter 5 covers In-Proc COM servers Chapter 6 covers Standalone COM servers Professional ATL COM Programming, Dr. Richard Grimes Chapter 7 covers both types of servers Note the discussion on “Single” vs “Apartment” MSDN – Search for STA, MTA, TNA, FTM Good articles from Don Box’s “House of COM”

Questions