examples/Base/IPC/condvar/condvarlocal/src/condvarlocal.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:
00028 Contains the E32Main() function, which executes the example.
00029 The example demonstrates the use of local condition variable IPC mechanism. The threads created in the process use this condition variable to co-operate with each other.
00030 The example performs the following tasks:
00031 - create an object of the CQueue class, which in turn crates a queue of integer tokens
00032 - create an object of the CProducer class, which in turn creates the producer thread
00033 - create an object of the CConsumer class, which in turn creates the consumer thread
00034 - create an object of the CUserInterface class ,which accepts asynchronous key press events from the console.
00035 The CUserInterface class provides an interface for the user to interact with the threads created in the example.
00036 The CQueue::iCondVar is the local condition varaibale that is used by the two threads to co-operate with each other.  
00037 */
00038 
00039 
00040 
00049 #include "globals.h"
00050 #include "queue.h"
00051 #include "producer.h"
00052 #include "consumer.h"
00053 #include "userinterface.h"
00054 
00055 LOCAL_D CConsoleBase* console;
00056 LOCAL_C void DoExampleL();
00057 LOCAL_C void callExampleL();
00058 
00059 LOCAL_C void DoExampleL()
00060         {
00061         CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
00062         CleanupStack::PushL(scheduler);
00063         CActiveScheduler::Install(scheduler);
00064 
00065         // Create an object of the CQueue class.
00066         CQueue* tokens = CQueue::NewL();
00067         CleanupStack::PushL(tokens);
00068 
00069         // Print the KTextInvokeProducer string and wait for a key press.
00070         _LIT(KTextInvokeProducer,"Creating and starting the Producer Thread...[This produces a token every two seconds]\n");
00071         console->Printf(KTextInvokeProducer);
00072         _LIT(KTxtPressAnyKeyToContinue,"[press any key to continue]\n");
00073         console->Printf(KTxtPressAnyKeyToContinue);
00074         console->Getch();
00075         // Create an object of the CProducer class.
00076         CProducer* prod = CProducer::NewL(console,tokens);
00077         CleanupStack::PushL(prod);
00078 
00079         // Print the KTextInvokeConsumer string and wait for a key press.
00080         _LIT(KTextInvokeConsumer,"Creating and starting the Consumer Thread...[This consumes a token every second]\n");
00081         console->Printf(KTextInvokeConsumer);
00082         console->Printf(KTxtPressAnyKeyToContinue);
00083         console->Getch();
00084 
00085         // Create an object of the CConsumer class.
00086         CConsumer* cons = CConsumer::NewL(console,tokens);
00087         CleanupStack::PushL(cons);
00088 
00089         // Create an object of the CUserInterface class.
00090         // This handles key press requests from the console.
00091         CUserInterface* ui = CUserInterface::NewL(console,prod);
00092         CleanupStack::PushL(ui);
00093         
00094         _LIT(KTextStartAS,"Starting the active scheduler...\n");
00095         console->Printf(KTextStartAS);
00096         // Issue an asynchronous read request.
00097         ui->ReadFunc();
00098         // Start the active scheduler.
00099         CActiveScheduler::Start();
00100 
00101         CleanupStack::PopAndDestroy(5,scheduler);
00102         }
00103 
00104 GLDEF_C TInt E32Main()
00105     {
00106         __UHEAP_MARK;
00107         CTrapCleanup* cleanup=CTrapCleanup::New();
00108         TRAPD(error,callExampleL());
00109         delete cleanup;
00110         __ASSERT_ALWAYS(!error,User::Panic(KTxtEPOC32EX,error));
00111         __UHEAP_MARKEND;
00112         return 0;
00113     }
00114 
00115 LOCAL_C void callExampleL()
00116     {
00117         console=Console::NewL(KTxtExampleCode,TSize(KConsFullScreen,KConsFullScreen));
00118         CleanupStack::PushL(console);
00119         TRAPD(error,DoExampleL());
00120         if (error)
00121                 console->Printf(KFormatFailed, error);
00122         else
00123                 console->Printf(KTxtOK);
00124         console->Printf(KTxtPressAnyKey);
00125         console->Getch();
00126         CleanupStack::PopAndDestroy();
00127     }

Generated by  doxygen 1.6.2