#ifndef init_MUTEX_LOCKED sema_init(&(pAd->usbdev_semaphore), 1); // linux kernel 2.6.38 sema_init(&(pAd->mlme_semaphore), 1); sema_init(&(pAd->RTUSBCmd_semaphore), 1); #else init_MUTEX_LOCKED(&(pAd->usbdev_semaphore)); // linux kernel 2.6.37 init_MUTEX_LOCKED(&(pAd->mlme_semaphore)); init_MUTEX_LOCKED(&(pAd->RTUSBCmd_semaphore)); #endif //Note: //currently typically to 1 (in which case the semaphore is called a mutex) //From Linus Torvalds /* Maximum concurrent users #define MAX_CONCURRENT_USERS 20 sema_init(&sem, MAX_CONCURRENT_USERS); However, almost all practical use of semaphores is a special case where the counter is initialized to 1, and where they are used as simple mutual exclusion with only one user allowed in the critical region. Such a semaphore is often called a "mutex" semaphore for MUTual EXclusion. */
Everything begins with object: object has properties (data fields) and behavior (functions/methods to set/get). Object is inside the layer like layers in Photoshop. This is my notebook mainly for object layer of C/C++,Java, JavaScript and PHP/MySQL/Apache/CSS/HTML but also many problems that I have to deal with everyday.
Thursday, July 19, 2012
linux Kernel 2.6.38 using sema_init
I just found out when upgraded rt73 device drivers that the old Linux kernel and the new version they changed the mutex to semaphore with extra parameter (A mutex is essentially the same thing as a binary semaphore(1 or 0 single resource) vs. counting values that
allows multiple program threads to share the same resource) I could not even
grep for the name because
the name is totally different. The name is now all low cases sema_init. So the device driver could
fail after upgrade to a new Linux kernel and this is the case we need to change the device driver rt73 module rtmp_init.c as follow.
Friday, July 13, 2012
LinkedList
In C++
/* * part1.h * */ #ifndef PART1_H_ #define PART1_H_ #pragma once //--------------------------------------------------------------------------- struct CarPart { long PartNumber; char PartName[40]; double UnitPrice; CarPart* Next; }; //--------------------------------------------------------------------------- class ListOfParts { int size; public: ListOfParts(); int Count(); CarPart* Head; int Add(CarPart* Item); CarPart *Retrieve(int pos); bool Delete(); }; #endif /* PART1_H_ */ #include <iostream> #include "parts1.h" using namespace std; //otherwise std::cout //--------------------------------------------------------------------------- ListOfParts::ListOfParts() //constructor // : size(0), Head(NULL) { size = 0; Head = NULL; // either way : size(0), Head(BULL) } //--------------------------------------------------------------------------- int ListOfParts::Count() { return size; } //--------------------------------------------------------------------------- int ListOfParts::Add(CarPart *NewItem) { CarPart *Node = new CarPart; //add struct CarPart into the class Node = NewItem; Node->Next = Head; //of the struct pointer Next Head = Node; // of class pointer Head return size++; } //--------------------------------------------------------------------------- CarPart *ListOfParts::Retrieve(int Position) { CarPart *Current = Head; for(int i = 0; i < Position && Current != NULL; i++) { Current = Current->Next; } return Current; } //--------------------------------------------------------------------------- bool ListOfParts::Delete() { if( Head == NULL ) { cout << "The list is empty\n"; return false; } else { CarPart *Current; Current = Head->Next; Head->Next = Current->Next; size--; return true; } } #include <iostream> #include <string.h> #include <stdio.h> #include "parts1.h" using namespace std; //--------------------------------------------------------------------------- int main() { ListOfParts *Parts = new ListOfParts(); //class CarPart *Part; //struct // Visual C++ 6 can't "scope" a variable in a for loop int i; Part = new CarPart; Part->PartNumber = 9743; strcpy(Part->PartName, "Air Filter"); Part->UnitPrice = 8.75; Parts->Add(Part); Part = new CarPart; Part->PartNumber = 27487; strcpy(Part->PartName, "Clutch Disk"); Part->UnitPrice = 47.15; Parts->Add(Part); Part = new CarPart; Part->PartNumber = 87873; strcpy(Part->PartName, "Brake Disk"); Part->UnitPrice = 35.15; Parts->Add(Part); Part = new CarPart; Part->PartNumber = 27644; strcpy(Part->PartName, "A/C Filter Drier"); Part->UnitPrice = 55.55; Parts->Add(Part); cout << "Number of Parts: " << Parts->Count() << endl; cout << "\n-=- List of Parts -=-"; for(i = 0; i < Parts->Count(); i++) { CarPart* One = Parts->Retrieve(i); cout << "\nCar Part Information"; cout << "\nPart #: " << One->PartNumber; cout << "\nDescription: " << One->PartName; cout << "\nUnit Price: $" << One->UnitPrice << endl; } Parts->Delete(); cout << "\nNumber of Parts: " << Parts->Count() << endl; cout << "\n-=- List of Parts -=-"; for(i = 0; i < Parts->Count(); i++) { CarPart* One = Parts->Retrieve(i); cout << "\nCar Part Information"; cout << "\nPart #: " << One->PartNumber; cout << "\nDescription: " << One->PartName; cout << "\nUnit Price: $" << One->UnitPrice << endl; } return 0; } Output: Number of Parts: 4 -=- List of Parts -=- Car Part Information Part #: 27644 Description: A/C Filter Drier Unit Price: $55.55 Car Part Information Part #: 87873 Description: Brake Disk Unit Price: $35.15 Car Part Information Part #: 27487 Description: Clutch Disk Unit Price: $47.15 Car Part Information Part #: 9743 Description: Air Filter Unit Price: $8.75 Number of Parts: 3 -=- List of Parts -=- Car Part Information Part #: 27644 Description: A/C Filter Drier Unit Price: $55.55 Car Part Information Part #: 27487 Description: Clutch Disk Unit Price: $47.15 Car Part Information Part #: 9743 Description: Air Filter Unit Price: $8.75In Java, it is much simple with a lot of less lines. Java has so many rules but it can be easy when you are master of the rules. I try to output the same format as C++. But it does more for example removeLast/removeFirst/getFirst/getLast item of the list and so on....LinkedList is a part of java.util.* so it is much easy.
//http://www.javatutorialhub.com/java-arrays.html import java.util.*; class CarPart{ long PartNumber; String PartName; double UnitPrice; CarPart(int i, String s, double j){ PartNumber = i; PartName = s; UnitPrice = j; } public String toString() { System.out.println("Car Part Information"); return "Part #: "+ PartNumber + "\n" +"Description: "+ PartName + "\n" + "Unit Price " + UnitPrice; } } class LinkedListPart{ public static void main(String args[]){ LinkedList<CarPart> parts = new LinkedList<CarPart>(); parts.add(new CarPart(9743,"Air Filter",8.75)); parts.add(new CarPart(27487,"Clutch Disk",47.15)); parts.add(new CarPart(87873,"Brake Disk",35.15)); parts.add(new CarPart(27644,"A/C Filter Drier",55.55)); System.out.println("Number of Parts " + parts.size()); for (CarPart element : parts) System.out.println(element + "\n"); parts.remove(0); System.out.println("--After remove first part from LinkedList---"); for (CarPart element : parts) System.out.println(element + "\n"); parts.removeLast(); System.out.println("---After remove last part from LinkedList---"); for (CarPart element : parts) System.out.println(element + "\n"); } } Output: Number of Parts 4 Car Part Information Part #: 9743 Description: Air Filter Unit Price 8.75 Car Part Information Part #: 27487 Description: Clutch Disk Unit Price 47.15 Car Part Information Part #: 87873 Description: Brake Disk Unit Price 35.15 Car Part Information Part #: 27644 Description: A/C Filter Drier Unit Price 55.55 --After remove first part from LinkedList--- Car Part Information Part #: 27487 Description: Clutch Disk Unit Price 47.15 Car Part Information Part #: 87873 Description: Brake Disk Unit Price 35.15 Car Part Information Part #: 27644 Description: A/C Filter Drier Unit Price 55.55 ---After remove last part from LinkedList--- Car Part Information Part #: 27487 Description: Clutch Disk Unit Price 47.15 Car Part Information Part #: 87873 Description: Brake Disk Unit Price 35.15
Tuesday, July 03, 2012
Why default constructor is required(explicitly) in a parent class if it has an argumented constructor
class A { A(int i){ } } class B extends A { } class Main { public static void main(String a[]){ B b_obj = new B(); } }This will be an error: cannot find symbol constructor in B If we don't have argument constructor the compiler will insert super() the first line in derived class constructor. Since the parent has parameter(arguments) constructor, it supersedes the default constructor in A, the compiler could not find the default constructor per error showing therefore so we need to insert the default constructor.
So class A will have 2 constructors: default A(){} and A(int i){}.
2 ways to fix it
- has a default constructor in parent class A
- has a super(int) in B constructor
class A { A(int i){ } A(){} //insert the default constructor in the base class, derived class unchanged } class B extends A { } class Main { public static void main(String a[]){ B b_obj = new B(); } } class A { A(int i){ } } class B extends A { B(int i) { // add constructor for B with super(i) in the 1st line super(i); } } class Main { public static void main(String a[]){ B b_obj = new B(2); //constructor has a value } }Same thing in C++ you need to create a default constructor for base class if the base class has arguments constructor as long as this argument/parameter constructor is exist the compile will not create default constructor. In Java you can use "super" keyword to call parameter constructor is must be the first line of derived/child constructor. To be compatible for both C++ and Java it is always good to have the parent default constructor.
class A { int i; public: A(){} A(int i){} //A(int i) no default constructor you have to create one above A(){} like Java }; class B : public A { public: }; int main(){ B *b_obj = new B; // for B compiler will use default constructor B(){} requires A(){} }See this for the full behaviors of C++ WRT constructors: http://en.wikipedia.org/wiki/Default_constructor
if you specify a constructor, the compiler will not create a default one for you. The compiler will define a default ctor if and only if you don't explicitly declare any ctors. It is confusing when there is a parent and child class (derived class). In the above examples, we think that the compiler will generate the default constructor for the child class since there is no constructor is defined so it creates a default constructor for the child but when the object is created in the child class, the parent constructor is called first then the child constructor, this is where the error happens because it could not find the default constructor in the parent class.
This rule applies to Java and C++ as well.
Another way to do is let the child class initializes the base class ctor then no default ctor at base class as long as the compiler know there is one ctor is defined so it does not look for a default ctor. This is the case there is no source code for base class just a library to link and it can be set in the derived class (child class)
#include <iostream> using namespace std; class A { int i; public: //A(){} A(int i){} //A(int i) no default constructor you have to create one above A(){} like Java }; class B : public A { public: int j; B():A(j){} // derived class initialize the base class constructor }; int main(){ B *b_obj = new B; }
Subscribe to:
Posts (Atom)